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