2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
17 Moment::Moment (int m
)
19 main_part_
= Rational (m
);
20 grace_part_
= Rational (0);
23 Moment::Moment (Rational m
, Rational g
)
29 Moment::Moment (Rational m
)
32 grace_part_
= Rational (0);
35 #include "ly-smobs.icc"
37 IMPLEMENT_SIMPLE_SMOBS (Moment
);
38 IMPLEMENT_TYPE_P (Moment
, "ly:moment?");
41 Moment::mark_smob (SCM
)
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
);
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 ()));
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
;
79 compare (Moment
const &a
, Moment
const &b
)
81 return Moment::compare (a
, b
);
85 Moment::compare (Moment
const &a
, Moment
const &b
)
87 int c
= Rational::compare (a
.main_part_
, b
.main_part_
);
91 return Rational::compare (a
.grace_part_
, b
.grace_part_
);
95 Moment::operator += (Moment
const &src
)
97 main_part_
+= src
.main_part_
;
98 grace_part_
+= src
.grace_part_
;
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. */
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. */
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. */
126 Moment::operator %= (Moment
const &src
)
128 main_part_
%= src
.main_part_
;
129 grace_part_
%= src
.main_part_
;
135 /* TODO: ensure MSB == 0 here */
136 return main_part_
.den ();
142 return main_part_
.num ();
146 Moment::to_bool () const
148 return main_part_
|| grace_part_
;
152 Moment::set_infinite (int k
)
154 main_part_
.set_infinite (k
);
158 Moment::to_string () const
160 string s
= main_part_
.to_string ();
162 s
+= "G" + grace_part_
.to_string ();
167 Moment::operator - () const
170 m
.grace_part_
= -grace_part_
;
171 m
.main_part_
= -main_part_
;
175 #ifdef STREAM_SUPPORT
177 operator << (ostream
&os
, Moment
const &m
)
179 os
<< m
.to_string ();
185 robust_scm2moment (SCM m
, Moment d
)
187 Moment
*p
= unsmob_moment (m
);
195 moment_less (SCM a
, SCM b
)
197 return *unsmob_moment (a
) < *unsmob_moment (b
);