number_impl.hpp

Go to the documentation of this file.
00001 #ifndef NUMBER_IMPL_HPP_
00002 #define NUMBER_IMPL_HPP_
00003 
00004 #include <iosfwd>
00005 #include <string>
00006 
00007 #include "rational.hpp"
00008 
00009 class number_impl
00010 {
00011 public:
00012   number_impl() : refcount_(1) {}
00013   virtual ~number_impl();
00014 
00016   void print(std::ostream& stream) const;
00018   std::string to_string() const;
00019 
00021   void save(std::ostream& stream) const;
00023   static number_impl* read_library(std::istream& stream);
00024 
00025   bool equals(number_impl const& rhs) const;
00026   bool less(number_impl const& rhs) const;
00027 
00028   number_impl* add(number_impl const& rhs);
00029   number_impl* subtract(number_impl const& rhs);
00030   number_impl* multiply(number_impl const& rhs);
00031   number_impl* divide(number_impl const& rhs);
00032 
00033   void add_ref();
00034   void del_ref();
00035 
00046   number_impl* promote(number_impl& rhs) const;
00047 
00048   virtual number_impl* promote_to_void();
00049   virtual number_impl* promote_to_long();
00050   virtual number_impl* promote_to_rational();
00051   virtual number_impl* promote_to_double();
00052 
00053 private:
00054   number_impl(number_impl const& n); 
00055   number_impl& operator=(number_impl const& n); 
00056 
00057   virtual std::string do_to_string() const = 0;
00058   virtual void do_save(std::ostream& stream) const = 0;
00059 
00060   virtual bool do_equals(number_impl const& rhs) const = 0;
00061   virtual bool do_less(number_impl const& rhs) const = 0;
00062 
00063   virtual number_impl* do_add(number_impl const& rhs) = 0;
00064   virtual number_impl* do_subtract(number_impl const& rhs) = 0;
00065   virtual number_impl* do_multiply(number_impl const& rhs) = 0;
00066   virtual number_impl* do_divide(number_impl const& rhs) = 0;
00067 
00068   virtual number_impl* do_promote(number_impl& rhs) const = 0;
00069 
00070   std::size_t refcount_;
00071 };
00072 
00077 class number_void : public number_impl {
00078 public:
00079   number_void() : number_impl() {}
00080 private:
00081   virtual std::string do_to_string() const;
00082   virtual void do_save(std::ostream& stream) const;
00083   virtual bool do_equals(number_impl const& rhs) const;
00084   virtual bool do_less(number_impl const& rhs) const;
00085   virtual number_impl* do_add(number_impl const& rhs);
00086   virtual number_impl* do_subtract(number_impl const& rhs);
00087   virtual number_impl* do_multiply(number_impl const& rhs);
00088   virtual number_impl* do_divide(number_impl const& rhs);
00089   virtual number_impl* do_promote(number_impl& rhs) const;
00090 };
00091 
00095 class number_long : public number_impl {
00096 public:
00097   number_long(long value) : number_impl(), value_(value) {}
00098   long value() const { return value_; }
00099   virtual number_impl* promote_to_void();
00100   virtual number_impl* promote_to_rational();
00101   virtual number_impl* promote_to_double();
00102 private:
00103   virtual std::string do_to_string() const;
00104   virtual void do_save(std::ostream& stream) const;
00105   virtual bool do_equals(number_impl const& rhs) const;
00106   virtual bool do_less(number_impl const& rhs) const;
00107   virtual number_impl* do_add(number_impl const& rhs);
00108   virtual number_impl* do_subtract(number_impl const& rhs);
00109   virtual number_impl* do_multiply(number_impl const& rhs);
00110   virtual number_impl* do_divide(number_impl const& rhs);
00111   virtual number_impl* do_promote(number_impl& rhs) const;
00112 
00113   long value_;
00114 };
00115 
00119 class number_rational: public number_impl {
00120 public:
00121   number_rational(rational<long> value) : number_impl(), value_(value) {}
00122   number_rational(long num) : number_impl(), value_(num) {}
00123   number_rational(long num, long den) : number_impl(), value_(num, den) {}
00124   rational<long> value() const { return value_; }
00125   virtual number_impl* promote_to_void();
00126   virtual number_impl* promote_to_double();
00127 private:
00128   virtual std::string do_to_string() const;
00129   virtual void do_save(std::ostream& stream) const;
00130   virtual bool do_equals(number_impl const& rhs) const;
00131   virtual bool do_less(number_impl const& rhs) const;
00132   virtual number_impl* do_add(number_impl const& rhs);
00133   virtual number_impl* do_subtract(number_impl const& rhs);
00134   virtual number_impl* do_multiply(number_impl const& rhs);
00135   virtual number_impl* do_divide(number_impl const& rhs);
00136   virtual number_impl* do_promote(number_impl& rhs) const;
00137 
00138   rational<long> value_;
00139 };
00140 
00144 class number_double : public number_impl {
00145 public:
00146   number_double(double value) : number_impl(), value_(value) {}
00147   double value() const { return value_; }
00148   virtual number_impl* promote_to_void();
00149 private:
00150   virtual std::string do_to_string() const;
00151   virtual void do_save(std::ostream& stream) const;
00152   virtual bool do_equals(number_impl const& rhs) const;
00153   virtual bool do_less(number_impl const& rhs) const;
00154   virtual number_impl* do_add(number_impl const& rhs);
00155   virtual number_impl* do_subtract(number_impl const& rhs);
00156   virtual number_impl* do_multiply(number_impl const& rhs);
00157   virtual number_impl* do_divide(number_impl const& rhs);
00158   virtual number_impl* do_promote(number_impl& rhs) const;
00159 
00160   double  value_;
00161 };
00162 
00163 #endif

Generated on Sun Nov 30 10:06:52 2008 for Calculator by  doxygen 1.5.3