2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2005 Han-Wen Nienhuys <hanwen@cs.uu.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"
36 IMPLEMENT_SIMPLE_SMOBS (Moment);
37 IMPLEMENT_TYPE_P (Moment, "ly:moment?");
40 Moment::mark_smob (SCM)
46 Moment::print_smob (SCM s, SCM port, scm_print_state *)
48 Moment *r = (Moment *) SCM_CELL_WORD_1 (s);
50 scm_puts ("#<Mom ", port);
51 String str = r->to_string ();
52 scm_puts ((char *)str.to_str0 (), port);
59 Moment::as_scheme () const
61 return scm_list_5 (ly_symbol2scm ("ly:make-moment"),
62 scm_from_int (main_part_.num ()),
63 scm_from_int (main_part_.den ()),
64 scm_from_int (grace_part_.num ()),
65 scm_from_int (grace_part_.den ()));
69 Moment::equal_p (SCM a, SCM b)
71 Moment *m1 = unsmob_moment (a);
72 Moment *m2 = unsmob_moment (b);
74 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
78 compare (Moment const &a, Moment const &b)
80 return Moment::compare (a, b);
84 Moment::compare (Moment const &a, Moment const &b)
86 int c = Rational::compare (a.main_part_, b.main_part_);
90 return Rational::compare (a.grace_part_, b.grace_part_);
94 Moment::operator += (Moment const &src)
96 main_part_ += src.main_part_;
97 grace_part_ += src.grace_part_;
101 Moment::operator -= (Moment const &src)
103 main_part_ -= src.main_part_;
104 grace_part_ -= src.grace_part_;
107 /* Only take the main part of SRC for multiplication. */
109 Moment::operator *= (Moment const &src)
111 main_part_ *= src.main_part_;
112 grace_part_ *= src.main_part_;
115 /* Only take the main part of SRC for division. */
117 Moment::operator /= (Moment const &src)
119 main_part_ /= src.main_part_;
120 grace_part_ /= src.main_part_;
123 /* Only take the main part of SRC for division. */
125 Moment::operator %= (Moment const &src)
127 main_part_ %= src.main_part_;
128 grace_part_ %= src.main_part_;
134 return main_part_.den ();
140 return main_part_.num ();
144 Moment::to_bool () const
146 return main_part_ || grace_part_;
150 Moment::set_infinite (int k)
152 main_part_.set_infinite (k);
156 Moment::to_string () const
158 String s = main_part_.to_string ();
161 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 << (std::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);