Bump version.
[lilypond.git] / lily / prob.cc
blob4d11d193f1b99f6068601131e75833276d15e3c6
1 /*
2 prob.cc -- implement Prob
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "prob.hh"
10 #include "main.hh"
11 #include "item.hh"
12 #include "input.hh"
13 #include "profile.hh"
15 #include "ly-smobs.icc"
17 IMPLEMENT_SMOBS (Prob);
18 IMPLEMENT_TYPE_P (Prob, "ly:prob?");
20 SCM
21 Prob::equal_p (SCM sa, SCM sb)
23 /* This comparison function is only designed to make the copy
24 constructor preserve equality.
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.
30 Prob *probs[2] = {unsmob_prob (sa), unsmob_prob (sb)};
31 SCM props[2][2];
32 int i;
34 for (i = 0; i < 2; i++)
36 props[i][0] = probs[i]->immutable_property_alist_;
37 props[i][1] = probs[i]->mutable_property_alist_;
40 if (strcmp (probs[0]->class_name (), probs[1]->class_name ()))
41 return SCM_BOOL_F;
43 /* Compare mutable and immutable lists, element by element. */
44 for (i = 0; i < 2; i++)
46 SCM aprop = props[0][i];
47 SCM bprop = props[1][i];
49 for (;
50 scm_is_pair (aprop) && scm_is_pair (bprop);
51 aprop = scm_cdr (aprop), bprop = scm_cdr (bprop))
53 SCM aval = scm_cdar (aprop);
54 SCM bval = scm_cdar (bprop);
55 if (scm_caar (aprop) != scm_caar (bprop) ||
57 !(unsmob_input (aval) && unsmob_input (bval))
58 &&
59 !to_boolean (scm_equal_p (aval, bval))))
60 return SCM_BOOL_F;
63 /* is one list shorter? */
64 if (aprop != SCM_EOL || bprop != SCM_EOL)
65 return SCM_BOOL_F;
68 return SCM_BOOL_T;
71 Prob::Prob (SCM type, SCM immutable_init)
73 self_scm_ = SCM_EOL;
74 mutable_property_alist_ = SCM_EOL;
75 immutable_property_alist_ = immutable_init;
76 type_ = type;
77 smobify_self ();
81 Prob::~Prob ()
85 Prob::Prob (Prob const &src)
87 immutable_property_alist_ = src.immutable_property_alist_;
88 mutable_property_alist_ = SCM_EOL;
89 self_scm_ = SCM_EOL;
90 type_ = src.type_;
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 ();
101 Prob::copy_mutable_properties () const
103 return ly_deep_copy (mutable_property_alist_);
106 void
107 Prob::derived_mark () const
112 Prob::mark_smob (SCM smob)
114 ASSERT_LIVE_IS_ALLOWED ();
116 Prob *system = (Prob *) SCM_CELL_WORD_1 (smob);
117 scm_gc_mark (system->mutable_property_alist_);
118 system->derived_mark ();
120 return system->immutable_property_alist_;
124 Prob::print_smob (SCM smob, SCM port, scm_print_state*)
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);
135 scm_puts (" >\n", port);
136 return 1;
142 Prob::internal_get_property (SCM sym) const
144 #ifndef NDEBUG
145 if (profile_property_accesses)
146 note_property_access (&prob_property_lookup_table, sym);
147 #endif
150 TODO: type checking
152 SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
153 if (s != SCM_BOOL_F)
154 return scm_cdr (s);
156 s = scm_sloppy_assq (sym, immutable_property_alist_);
157 return (s == SCM_BOOL_F) ? SCM_EOL : scm_cdr (s);
160 /* We don't (yet) instrument probs */
161 void
162 Prob::instrumented_set_property (SCM sym, SCM val, const char*, int, const char*)
164 internal_set_property (sym, val);
167 void
168 Prob::internal_set_property (SCM sym, SCM val)
170 if (do_internal_type_checking_global)
171 type_check_assignment (sym, val);
173 mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, val);
176 void
177 Prob::type_check_assignment (SCM, SCM) const
179 /* empty */
183 Prob::get_property_alist (bool m) const
185 return (m) ? mutable_property_alist_ : immutable_property_alist_;
188 string
189 Prob::name () const
191 SCM nm = get_property ("name");
192 if (scm_is_symbol (nm))
193 return ly_symbol2string (nm);
194 else
195 return this->class_name ();