]> git.donarmstrong.com Git - lilypond.git/blob - lily/prob.cc
Run `make grand-replace'.
[lilypond.git] / lily / prob.cc
1 /*
2   prob.cc -- implement Prob
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2008 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "prob.hh"
10 #include "main.hh"
11 #include "item.hh"
12 #include "input.hh"
13 #include "profile.hh"
14
15 #include "ly-smobs.icc"
16
17 IMPLEMENT_SMOBS (Prob);
18 IMPLEMENT_TYPE_P (Prob, "ly:prob?");
19
20 SCM
21 Prob::equal_p (SCM sa, SCM sb)
22 {
23   /* This comparison function is only designed to make the copy
24      constructor preserve equality.
25
26      Perhaps it would be better to use a more strict definition of
27      equality; e.g., that that two probs are equal iff they can be
28      distinguished by calls to ly:prob-property.
29   */
30   Prob *probs[2] = {unsmob_prob (sa), unsmob_prob (sb)};
31   SCM props[2][2];
32   int i;
33
34   for (i = 0; i < 2; i++)
35     {
36       props[i][0] = probs[i]->immutable_property_alist_;
37       props[i][1] = probs[i]->mutable_property_alist_;
38     }
39
40   if (strcmp (probs[0]->class_name (), probs[1]->class_name ()))
41     return SCM_BOOL_F;
42
43   /* Compare mutable and immutable lists, element by element. */
44   for (i = 0; i < 2; i++)
45     {
46       SCM aprop = props[0][i];
47       SCM bprop = props[1][i];
48
49       for (;
50            scm_is_pair (aprop) && scm_is_pair (bprop);
51            aprop = scm_cdr (aprop), bprop = scm_cdr (bprop))
52         {
53           SCM aval = scm_cdar (aprop);
54           SCM bval = scm_cdar (bprop);
55           if (scm_caar (aprop) != scm_caar (bprop) ||
56               (
57                !(unsmob_input (aval) && unsmob_input (bval))
58                &&                
59                !to_boolean (scm_equal_p (aval, bval))))
60             return SCM_BOOL_F;
61         }
62
63       /* is one list shorter? */
64       if (aprop != SCM_EOL || bprop != SCM_EOL)
65         return SCM_BOOL_F;
66     }
67
68   return SCM_BOOL_T;
69 }
70
71 Prob::Prob (SCM type, SCM immutable_init)
72 {
73   self_scm_ = SCM_EOL;
74   mutable_property_alist_ = SCM_EOL;
75   immutable_property_alist_ = immutable_init;
76   type_ = type;
77   smobify_self ();
78 }
79
80
81 Prob::~Prob ()
82 {
83 }
84
85 Prob::Prob (Prob const &src)
86 {
87   immutable_property_alist_ = src.immutable_property_alist_;
88   mutable_property_alist_ = SCM_EOL;
89   self_scm_ = SCM_EOL;
90   type_ = src.type_;
91
92   /* First we smobify_self, then we copy over the stuff.  If we don't,
93      stack vars that hold the copy might be optimized away, meaning
94      that they won't be protected from GC. */
95   smobify_self ();
96   mutable_property_alist_ = src.copy_mutable_properties ();
97 }
98
99
100 SCM
101 Prob::copy_mutable_properties () const
102 {
103   return ly_deep_copy (mutable_property_alist_);
104 }
105
106 void
107 Prob::derived_mark () const
108 {
109 }
110
111 SCM
112 Prob::mark_smob (SCM smob)
113 {
114   ASSERT_LIVE_IS_ALLOWED ();
115   
116   Prob *system = (Prob *) SCM_CELL_WORD_1 (smob);
117   scm_gc_mark (system->mutable_property_alist_);
118   system->derived_mark ();
119   
120   return system->immutable_property_alist_;
121 }
122
123 int
124 Prob::print_smob (SCM smob, SCM port, scm_print_state*)
125 {
126   Prob *p = (Prob *) SCM_CELL_WORD_1 (smob);
127   scm_puts ("#<", port);
128   scm_puts ("Prob: ", port);
129   scm_display (p->type_, port);
130   scm_puts (" C++: ", port);
131   scm_puts (p->class_name (), port);
132   scm_display (p->mutable_property_alist_, port);
133   scm_display (p->immutable_property_alist_, port);
134   
135   scm_puts (" >\n", port);
136   return 1;
137 }
138
139
140
141 SCM
142 Prob::internal_get_property (SCM sym) const
143 {
144 #ifndef NDEBUG
145   if (profile_property_accesses)
146     note_property_access (&prob_property_lookup_table, sym);
147 #endif
148
149   /*
150     TODO: type checking
151    */
152   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
153   if (s != SCM_BOOL_F)
154     return scm_cdr (s);
155
156   s = scm_sloppy_assq (sym, immutable_property_alist_);
157   return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
158 }
159
160 /* We don't (yet) instrument probs */
161 void
162 Prob::instrumented_set_property (SCM sym, SCM val, const char*, int, const char*)
163 {
164   internal_set_property (sym, val);
165 }
166
167 void
168 Prob::internal_set_property (SCM sym, SCM val) 
169 {
170   if (do_internal_type_checking_global)
171     type_check_assignment (sym, val);
172   
173   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, val);
174 }
175
176 void
177 Prob::type_check_assignment (SCM sym, SCM val) const
178 {
179   (void) sym;
180   (void) val;
181 }
182
183 SCM
184 Prob::get_property_alist (bool m) const
185 {
186   return (m) ? mutable_property_alist_ : immutable_property_alist_;
187 }
188
189 string
190 Prob::name () const
191 {
192   SCM nm = get_property ("name");
193   if (scm_is_symbol (nm))
194     return ly_symbol2string (nm);
195   else
196     return this->class_name ();
197 }
198