lilypond-1.5.10
[lilypond.git] / lily / moment.cc
blobe75b6880f6958acc8319de3175963ddd6f2f7eb8
1 /*
2 moment.cc -- implement Moment
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
11 #include "lily-guile.hh"
12 #include "moment.hh"
13 #include "warn.hh"
14 #include "ly-smobs.icc"
16 IMPLEMENT_UNSMOB (Moment,moment);
17 IMPLEMENT_SIMPLE_SMOBS (Moment);
18 IMPLEMENT_TYPE_P (Moment, "moment?");
20 SCM
21 Moment::mark_smob (SCM)
23 return SCM_EOL;
27 SCM
28 Moment::smobbed_copy () const
30 Moment * m = new Moment (*this);
31 return m->smobbed_self ();
35 int
36 Moment::print_smob (SCM s, SCM port, scm_print_state *)
38 Moment *r = (Moment *) gh_cdr (s);
40 scm_puts ("#<Mom ", port);
41 String str (r->str ());
42 scm_puts ((char *)str.ch_C (), port);
43 scm_puts (" >", port);
45 return 1;
49 TODO: add optional factor argument.
51 SCM
52 make_rational (SCM n, SCM d)
54 Moment m (Rational (1,1));
56 if (SCM_INUMP (n) && SCM_INUMP (d))
58 m = Moment (Rational (gh_scm2int (n), gh_scm2int (d)));
60 else
62 ::error ("make-moment takes two integer arguments. Using 1/1");
65 return m.smobbed_copy ();
69 void
70 init_moments ()
72 scm_c_define_gsubr ("make-moment", 2 , 0, 0, (Scheme_function_unknown) make_rational);
75 ADD_SCM_INIT_FUNC (moms,init_moments);
77 SCM
78 Moment::equal_p (SCM a, SCM b)
80 Moment *m1 = unsmob_moment (a);
81 Moment *m2 = unsmob_moment (b);
83 return (*m1 == *m2) ? SCM_BOOL_T : SCM_BOOL_F;
86 /****************************************************************/
88 int
89 compare (Moment const &a, Moment const &b)
91 return Moment::compare (a,b);
94 int
95 Moment::compare (Moment const &a, Moment const &b)
97 int c = Rational::compare (a.main_part_,b.main_part_);
98 if (c)
99 return c;
101 return Rational::compare (a.grace_part_, b.grace_part_);
104 Moment::Moment ()
109 Moment::Moment (int m)
111 main_part_ = Rational(m);
112 grace_part_ = Rational( 0);
115 Moment::Moment (Rational m, Rational g)
117 main_part_ = m;
118 grace_part_ = g;
121 Moment::Moment (Rational m)
123 main_part_ = m;
124 grace_part_ = Rational (0);
127 void
128 Moment::operator += (Moment const &src)
130 main_part_ +=src.main_part_ ;
131 grace_part_ += src.grace_part_;
133 void
134 Moment::operator -= (Moment const &src)
136 main_part_ -= src.main_part_ ;
137 grace_part_ -= src.grace_part_;
141 only take the main part of SRC for multiplication.
143 void
144 Moment::operator *= (Moment const &src)
146 main_part_ *= src.main_part_ ;
147 grace_part_ *= src.main_part_;
151 only take the main part of SRC for multiplication.
153 void
154 Moment::operator /= (Moment const &src)
156 main_part_ /= src.main_part_ ;
157 grace_part_ /= src.main_part_;
161 #if 0
162 Moment::operator Rational()
164 return main_part_;
166 #endif
169 Moment::den () const { return main_part_.den (); }
172 Moment::num () const { return main_part_.num (); }
174 bool
175 Moment::to_bool () const
177 return main_part_ || grace_part_;
180 void
181 Moment::set_infinite (int k)
183 main_part_.set_infinite (k);
187 String
188 Moment::str () const
190 String s = main_part_.str ();
191 if (grace_part_)
193 s += "G" + grace_part_.str ();
195 return s;
198 Moment
199 Moment::operator - () const
201 Moment m;
202 m.grace_part_ = -grace_part_;
203 m.main_part_ = -main_part_;
204 return m;
207 ostream &
208 operator << (ostream &os, Moment const &m)
210 os << m.str ();
211 return os;