Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / moment.cc
blob3ca7e520db3be7dc3178abcc36bb9628b92eaf53
1 /*
2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "moment.hh"
11 #include "warn.hh"
13 Moment::Moment ()
17 Moment::Moment (int m)
19 main_part_ = Rational (m);
20 grace_part_ = Rational (0);
23 Moment::Moment (Rational m, Rational g)
25 main_part_ = m;
26 grace_part_ = g;
29 Moment::Moment (Rational m)
31 main_part_ = m;
32 grace_part_ = Rational (0);
35 #include "ly-smobs.icc"
37 IMPLEMENT_SIMPLE_SMOBS (Moment);
38 IMPLEMENT_TYPE_P (Moment, "ly:moment?");
40 SCM
41 Moment::mark_smob (SCM)
43 return SCM_EOL;
46 int
47 Moment::print_smob (SCM s, SCM port, scm_print_state *)
49 Moment *r = (Moment *) SCM_CELL_WORD_1 (s);
51 scm_puts ("#<Mom ", port);
52 string str = r->to_string ();
53 scm_puts ((char *)str.c_str (), port);
54 scm_puts (">", port);
56 return 1;
59 SCM
60 Moment::as_scheme () const
62 return scm_list_5 (ly_symbol2scm ("ly:make-moment"),
63 scm_from_int64 (main_part_.num ()),
64 scm_from_int64 (main_part_.den ()),
65 scm_from_int64 (grace_part_.num ()),
66 scm_from_int64 (grace_part_.den ()));
69 SCM
70 Moment::equal_p (SCM a, SCM b)
72 Moment *m1 = unsmob_moment (a);
73 Moment *m2 = unsmob_moment (b);
75 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
78 int
79 compare (Moment const &a, Moment const &b)
81 return Moment::compare (a, b);
84 int
85 Moment::compare (Moment const &a, Moment const &b)
87 int c = Rational::compare (a.main_part_, b.main_part_);
88 if (c)
89 return c;
91 return Rational::compare (a.grace_part_, b.grace_part_);
94 void
95 Moment::operator += (Moment const &src)
97 main_part_ += src.main_part_;
98 grace_part_ += src.grace_part_;
101 void
102 Moment::operator -= (Moment const &src)
104 main_part_ -= src.main_part_;
105 grace_part_ -= src.grace_part_;
108 /* Only take the main part of SRC for multiplication. */
109 void
110 Moment::operator *= (Moment const &src)
112 main_part_ *= src.main_part_;
113 grace_part_ *= src.main_part_;
116 /* Only take the main part of SRC for division. */
117 void
118 Moment::operator /= (Moment const &src)
120 main_part_ /= src.main_part_;
121 grace_part_ /= src.main_part_;
124 /* Only take the main part of SRC for division. */
125 void
126 Moment::operator %= (Moment const &src)
128 main_part_ %= src.main_part_;
129 grace_part_ %= src.main_part_;
133 Moment::den () const
135 /* TODO: ensure MSB == 0 here */
136 return main_part_.den ();
140 Moment::num () const
142 return main_part_.num ();
145 bool
146 Moment::to_bool () const
148 return main_part_ || grace_part_;
151 void
152 Moment::set_infinite (int k)
154 main_part_.set_infinite (k);
157 string
158 Moment::to_string () const
160 string s = main_part_.to_string ();
161 if (grace_part_)
162 s += "G" + grace_part_.to_string ();
163 return s;
166 Moment
167 Moment::operator - () const
169 Moment m;
170 m.grace_part_ = -grace_part_;
171 m.main_part_ = -main_part_;
172 return m;
175 #ifdef STREAM_SUPPORT
176 ostream &
177 operator << (ostream &os, Moment const &m)
179 os << m.to_string ();
180 return os;
182 #endif
184 Moment
185 robust_scm2moment (SCM m, Moment d)
187 Moment *p = unsmob_moment (m);
188 if (!p)
189 return d;
190 else
191 return *p;
194 bool
195 moment_less (SCM a, SCM b)
197 return *unsmob_moment (a) < *unsmob_moment (b);