1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 2004--2020 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 using std::string;
27 
28 const char *const Prob::type_p_name_ = "ly:prob?";
29 
30 SCM
equal_p(SCM sa,SCM sb)31 Prob::equal_p (SCM sa, SCM sb)
32 {
33   /* This comparison function is only designed to make the copy
34      constructor preserve equality.
35 
36      Perhaps it would be better to use a more strict definition of
37      equality; e.g., that two probs are equal iff they cannot be
38      distinguished by calls to ly:prob-property.
39   */
40   Prob *probs[2] = {unsmob<Prob> (sa), unsmob<Prob> (sb)};
41   SCM props[2][2];
42   int i;
43 
44   for (i = 0; i < 2; i++)
45     {
46       props[i][0] = probs[i]->immutable_property_alist_;
47       props[i][1] = probs[i]->mutable_property_alist_;
48     }
49 
50   if (strcmp (probs[0]->class_name (), probs[1]->class_name ()))
51     return SCM_BOOL_F;
52 
53   /* Compare mutable and immutable lists, element by element. */
54   for (i = 0; i < 2; i++)
55     {
56       SCM aprop = props[0][i];
57       SCM bprop = props[1][i];
58 
59       for (;; aprop = scm_cdr (aprop), bprop = scm_cdr (bprop))
60         {
61           SCM origin_sym = ly_symbol2scm ("origin");
62           // Skip over origin fields
63           while (scm_is_pair (aprop)
64                  && scm_is_eq (origin_sym, scm_caar (aprop)))
65             aprop = scm_cdr (aprop);
66 
67           while (scm_is_pair (bprop)
68                  && scm_is_eq (origin_sym, scm_caar (bprop)))
69             bprop = scm_cdr (bprop);
70 
71           /* is one list shorter? */
72           if (!scm_is_pair (aprop))
73             if (!scm_is_pair (bprop))
74               break;
75             else
76               return SCM_BOOL_F;
77           else if (!scm_is_pair (bprop))
78             return SCM_BOOL_F;
79 
80           SCM aval = scm_cdar (aprop);
81           SCM bval = scm_cdar (bprop);
82           if (!scm_is_eq (scm_caar (aprop), scm_caar (bprop))
83               || !ly_is_equal (aval, bval))
84             return SCM_BOOL_F;
85         }
86     }
87   return SCM_BOOL_T;
88 }
89 
Prob(SCM type,SCM immutable_init)90 Prob::Prob (SCM type, SCM immutable_init) : Smob<Prob> ()
91 {
92   mutable_property_alist_ = SCM_EOL;
93   immutable_property_alist_ = immutable_init;
94   type_ = type;
95   smobify_self ();
96 }
97 
~Prob()98 Prob::~Prob ()
99 {
100 }
101 
Prob(Prob const & src)102 Prob::Prob (Prob const &src)
103   : Smob<Prob> ()
104 {
105   immutable_property_alist_ = src.immutable_property_alist_;
106   mutable_property_alist_ = SCM_EOL;
107   type_ = src.type_;
108 
109   /* First we smobify_self, then we copy over the stuff.  If we don't,
110      stack vars that hold the copy might be optimized away, meaning
111      that they won't be protected from GC. */
112   smobify_self ();
113   mutable_property_alist_ = src.copy_mutable_properties ();
114 }
115 
116 SCM
copy_mutable_properties() const117 Prob::copy_mutable_properties () const
118 {
119   return ly_deep_copy (mutable_property_alist_);
120 }
121 
122 void
derived_mark() const123 Prob::derived_mark () const
124 {
125 }
126 
127 SCM
mark_smob() const128 Prob::mark_smob () const
129 {
130   ASSERT_LIVE_IS_ALLOWED (self_scm ());
131 
132   scm_gc_mark (mutable_property_alist_);
133   derived_mark ();
134 
135   return immutable_property_alist_;
136 }
137 
138 int
print_smob(SCM port,scm_print_state *) const139 Prob::print_smob (SCM port, scm_print_state *) const
140 {
141   scm_puts ("#<", port);
142   scm_puts ("Prob: ", port);
143   scm_display (type_, port);
144   scm_puts (" C++: ", port);
145   scm_puts (class_name (), port);
146   scm_display (mutable_property_alist_, port);
147   scm_display (immutable_property_alist_, port);
148 
149   scm_puts (" >\n", port);
150   return 1;
151 }
152 
153 SCM
internal_get_property(SCM sym) const154 Prob::internal_get_property (SCM sym) const
155 {
156 #ifdef DEBUG
157   if (profile_property_accesses)
158     note_property_access (&prob_property_lookup_table, sym);
159 #endif
160 
161   /*
162     TODO: type checking
163    */
164   SCM s = scm_sloppy_assq (sym, mutable_property_alist_);
165   if (scm_is_true (s))
166     return scm_cdr (s);
167 
168   s = scm_sloppy_assq (sym, immutable_property_alist_);
169   return scm_is_false (s) ? SCM_EOL : scm_cdr (s);
170 }
171 
172 /* We don't (yet) instrument probs */
173 void
instrumented_set_property(SCM sym,SCM val,const char *,int,const char *)174 Prob::instrumented_set_property (SCM sym, SCM val, const char *, int, const char *)
175 {
176   internal_set_property (sym, val);
177 }
178 
179 void
internal_set_property(SCM sym,SCM val)180 Prob::internal_set_property (SCM sym, SCM val)
181 {
182   if (do_internal_type_checking_global)
183     type_check_assignment (sym, val);
184 
185   mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, sym, val);
186 }
187 
188 void
type_check_assignment(SCM,SCM) const189 Prob::type_check_assignment (SCM, SCM) const
190 {
191   /* empty */
192 }
193 
194 SCM
get_property_alist(bool m) const195 Prob::get_property_alist (bool m) const
196 {
197   return (m) ? mutable_property_alist_ : immutable_property_alist_;
198 }
199 
200 string
name() const201 Prob::name () const
202 {
203   SCM nm = get_property (this, "name");
204   if (scm_is_symbol (nm))
205     return ly_symbol2string (nm);
206   else
207     return class_name ();
208 }
209