]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-property.cc
* lily/context.cc (where_defined): also assign value in
[lilypond.git] / lily / grob-property.cc
1 /*
2   Implement storage and manipulation of grob properties.
3 */
4
5 #include <cstring>
6 #include <math.h>
7
8 #include "main.hh"
9 #include "input-smob.hh"
10 #include "pointer-group-interface.hh"
11 #include "misc.hh"
12 #include "paper-score.hh"
13 #include "output-def.hh"
14 #include "spanner.hh"
15 #include "item.hh"
16 #include "misc.hh"
17 #include "item.hh"
18 #include "program-option.hh"
19
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   This special add_thing routine is slightly more efficient than
32
33   set_prop (name, cons (thing, get_prop (name)))
34
35   since it can reuse the handle returned by scm_assq ().
36 */
37 // JUNKME.
38 void
39 Grob::add_to_list_property (SCM sym, SCM thing)
40 {
41   SCM handle
42     = scm_sloppy_assq (sym, mutable_property_alist_);
43
44   if (handle != SCM_BOOL_F)
45     {
46       scm_set_cdr_x (handle, scm_cons (thing, scm_cdr (handle)));
47     }
48   else
49     {
50       /*
51         There is no mutable prop yet, so create an entry, and put it in front of the
52         mutable prop list.
53       */
54       handle = scm_sloppy_assq (sym, immutable_property_alist_);
55       SCM tail = (handle != SCM_BOOL_F) ? scm_cdr (handle) : SCM_EOL;
56       SCM val = scm_cons (thing, tail);
57
58       mutable_property_alist_ = scm_cons (scm_cons (sym, val),
59                                           mutable_property_alist_);
60     }
61 }
62
63
64
65 extern void check_interfaces_for_property (Grob const *me, SCM sym);
66
67 void
68 Grob::internal_set_property (SCM sym, SCM v)
69 {
70 #ifndef NDEBUG
71   SCM grob_p = ly_lily_module_constant ("ly:grob?");
72   SCM grob_list_p = ly_lily_module_constant ("grob-list?");
73   SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
74   
75   if (type == grob_p
76       || type == grob_list_p
77       || (unsmob_grob (v) && ly_symbol2scm ("cause") != sym))
78     {
79       scm_display (scm_list_2 (sym, type), scm_current_output_port());
80       assert (0);
81     }
82 #endif
83
84   /* Perhaps we simply do the assq_set, but what the heck. */
85   if (!is_live ())
86     return;
87
88   if (do_internal_type_checking_global)
89     {
90       if (!type_check_assignment (sym, v, ly_symbol2scm ("backend-type?")))
91         abort ();
92       check_interfaces_for_property (this, sym);
93     }
94
95   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, v);
96 }
97
98 //#define PROFILE_PROPERTY_ACCESSES
99
100 SCM grob_property_lookup_table;
101 LY_DEFINE(ly_property_lookup_stats, "ly:grob-property-lookup-stats",
102           0,0,0, (),
103           "")
104 {
105   return grob_property_lookup_table ?  grob_property_lookup_table :
106     scm_c_make_hash_table (1);
107 }
108
109 void
110 note_property_access (SCM *table, SCM sym)
111 {
112   /*
113     Statistics: which properties are looked up? 
114   */
115   if (!*table)
116     *table = scm_permanent_object (scm_c_make_hash_table (259));
117   
118   SCM hashhandle = scm_hashq_get_handle (*table, sym);
119   if (hashhandle == SCM_BOOL_F)
120     {
121       scm_hashq_set_x (*table, sym, scm_from_int (0));
122       hashhandle = scm_hashq_get_handle (*table, sym);
123     }
124
125   int count = scm_to_int (scm_cdr (hashhandle)) + 1;  
126   scm_set_cdr_x (hashhandle, scm_from_int (count));
127 }
128
129
130 SCM
131 Grob::internal_get_property (SCM sym) const
132 {
133 #ifndef NDEBUG
134   if (profile_property_accesses)
135     {
136       note_property_access (&grob_property_lookup_table, sym);
137     }
138 #endif
139   
140   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
141   if (s != SCM_BOOL_F)
142     return scm_cdr (s);
143   
144   s = scm_sloppy_assq (sym, immutable_property_alist_);
145
146   if (do_internal_type_checking_global && scm_is_pair (s))
147     {
148       if (!type_check_assignment (sym, scm_cdr (s),
149                                   ly_symbol2scm ("backend-type?")))
150         abort ();
151
152       check_interfaces_for_property (this, sym);
153     }
154
155   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
156 }
157
158
159
160 void
161 Grob::internal_set_object (SCM s, SCM v)
162 {
163   /* Perhaps we simply do the assq_set, but what the heck. */
164   if (!is_live ())
165     return;
166
167   object_alist_ = scm_assq_set_x (object_alist_, s, v);
168 }
169
170 SCM
171 Grob::internal_get_object (SCM sym) const
172 {
173 #ifdef PROFILE_PROPERTY_ACCESSES
174   note_property_access (&grob_property_lookup_table, sym);
175 #endif
176
177   SCM s = scm_sloppy_assq (sym, object_alist_);
178
179   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
180 }
181
182 void
183 Grob::substitute_object_links (SCM crit, SCM orig)
184 {
185   set_break_subsititution (crit);
186   object_alist_ = substitute_object_alist (orig, object_alist_);
187 }
188
189 bool
190 Grob::is_live () const
191 {
192   return immutable_property_alist_ != SCM_EOL;
193 }