2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
28 Moment::Moment (int m
)
30 main_part_
= Rational (m
);
31 grace_part_
= Rational (0);
34 Moment::Moment (Rational m
, Rational g
)
40 Moment::Moment (Rational m
)
43 grace_part_
= Rational (0);
46 #include "ly-smobs.icc"
48 IMPLEMENT_SIMPLE_SMOBS (Moment
);
49 IMPLEMENT_TYPE_P (Moment
, "ly:moment?");
52 Moment::mark_smob (SCM
)
58 Moment::print_smob (SCM s
, SCM port
, scm_print_state
*)
60 Moment
*r
= (Moment
*) SCM_CELL_WORD_1 (s
);
62 scm_puts ("#<Mom ", port
);
63 string str
= r
->to_string ();
64 scm_puts ((char *)str
.c_str (), port
);
71 Moment::as_scheme () const
73 return scm_list_5 (ly_symbol2scm ("ly:make-moment"),
74 scm_from_int64 (main_part_
.num ()),
75 scm_from_int64 (main_part_
.den ()),
76 scm_from_int64 (grace_part_
.num ()),
77 scm_from_int64 (grace_part_
.den ()));
81 Moment::equal_p (SCM a
, SCM b
)
83 Moment
*m1
= unsmob_moment (a
);
84 Moment
*m2
= unsmob_moment (b
);
86 return (*m1
== *m2
) ? SCM_BOOL_T
: SCM_BOOL_F
;
90 compare (Moment
const &a
, Moment
const &b
)
92 return Moment::compare (a
, b
);
96 Moment::compare (Moment
const &a
, Moment
const &b
)
98 int c
= Rational::compare (a
.main_part_
, b
.main_part_
);
102 return Rational::compare (a
.grace_part_
, b
.grace_part_
);
106 Moment::operator += (Moment
const &src
)
108 main_part_
+= src
.main_part_
;
109 grace_part_
+= src
.grace_part_
;
113 Moment::operator -= (Moment
const &src
)
115 main_part_
-= src
.main_part_
;
116 grace_part_
-= src
.grace_part_
;
119 /* Only take the main part of SRC for multiplication. */
121 Moment::operator *= (Moment
const &src
)
123 main_part_
*= src
.main_part_
;
124 grace_part_
*= src
.main_part_
;
127 /* Only take the main part of SRC for division. */
129 Moment::operator /= (Moment
const &src
)
131 main_part_
/= src
.main_part_
;
132 grace_part_
/= src
.main_part_
;
135 /* Only take the main part of SRC for division. */
137 Moment::operator %= (Moment
const &src
)
139 main_part_
%= src
.main_part_
;
140 grace_part_
%= src
.main_part_
;
146 /* TODO: ensure MSB == 0 here */
147 return main_part_
.den ();
153 return main_part_
.num ();
157 Moment::to_bool () const
159 return main_part_
|| grace_part_
;
163 Moment::set_infinite (int k
)
165 main_part_
.set_infinite (k
);
169 Moment::to_string () const
171 string s
= main_part_
.to_string ();
173 s
+= "G" + grace_part_
.to_string ();
178 Moment::operator - () const
181 m
.grace_part_
= -grace_part_
;
182 m
.main_part_
= -main_part_
;
186 #ifdef STREAM_SUPPORT
188 operator << (ostream
&os
, Moment
const &m
)
190 os
<< m
.to_string ();
196 robust_scm2moment (SCM m
, Moment d
)
198 Moment
*p
= unsmob_moment (m
);
206 moment_less (SCM a
, SCM b
)
208 return *unsmob_moment (a
) < *unsmob_moment (b
);