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