00001 #include "number.hpp" 00002 #include "number_impl.hpp" 00003 00004 #include <iostream> 00005 #include <ostream> 00006 00007 number::number() 00008 : pimpl_(new number_void()) 00009 {} 00010 00011 number::number(int x) 00012 : pimpl_(new number_long(x)) 00013 {} 00014 00015 number::number(long x) 00016 : pimpl_(new number_long(x)) 00017 {} 00018 00019 number::number(rational<long> const& x) 00020 : pimpl_(new number_rational(x)) 00021 {} 00022 00023 number::number(double x) 00024 : pimpl_(new number_double(x)) 00025 {} 00026 00027 number::number(number const& n) 00028 : pimpl_(n.pimpl_) 00029 { 00030 pimpl_->add_ref(); 00031 } 00032 00033 number::number(std::istream& stream) 00034 : pimpl_(number_impl::read_library(stream)) 00035 {} 00036 00037 number::number(number_impl* pimpl) 00038 : pimpl_(pimpl) 00039 {} 00040 00041 number::~number() 00042 { 00043 pimpl_->del_ref(); 00044 } 00045 00046 number& number::operator=(number const& that) 00047 { 00048 that.pimpl_->add_ref(); 00049 this->pimpl_->del_ref(); 00050 this->pimpl_ = that.pimpl_; 00051 return *this; 00052 } 00053 00054 void number::save(std::ostream& stream) 00055 const 00056 { 00057 pimpl_->save(stream); 00058 } 00059 00060 std::string number::to_string() 00061 const 00062 { 00063 return pimpl_->to_string(); 00064 } 00065 00066 void number::print(std::ostream& stream) 00067 { 00068 pimpl_->print(stream); 00069 } 00070 00071 void number::coerce(number& that) 00072 { 00073 // Promote that number to the type of this. 00074 that = number(this->pimpl_->promote(*that.pimpl_)); 00075 // Promote this to the type of that. 00076 *this = number(that.pimpl_->promote(*this->pimpl_)); 00077 } 00078 00079 bool number::equals(number b) 00080 { 00081 coerce(b); 00082 return pimpl_->equals(*b.pimpl_); 00083 } 00084 00085 bool number::less(number b) 00086 { 00087 coerce(b); 00088 return pimpl_->less(*b.pimpl_); 00089 } 00090 00091 number number::operator+(number rhs) 00092 { 00093 coerce(rhs); 00094 return number(pimpl_->add(*rhs.pimpl_)); 00095 } 00096 00097 number number::operator-(number rhs) 00098 { 00099 coerce(rhs); 00100 return number(pimpl_->subtract(*rhs.pimpl_)); 00101 } 00102 00103 number number::operator*(number rhs) 00104 { 00105 coerce(rhs); 00106 return number(pimpl_->multiply(*rhs.pimpl_)); 00107 } 00108 00109 number number::operator/(number rhs) 00110 { 00111 coerce(rhs); 00112 return number(pimpl_->divide(*rhs.pimpl_)); 00113 }