Calculator Step 6

- Main Page
- Classes
- Files

- File List
- File Members

number_impl.hpp
Go to the documentation of this file.
1 #ifndef NUMBER_IMPL_HPP_
2 #define NUMBER_IMPL_HPP_
3
4 #include <iosfwd>
5 #include <memory>
6 #include <string>
7
8 #include "rational.hpp"
9
10 class number_impl : public std::enable_shared_from_this<number_impl>
11 {
12 public:
13 virtual ~number_impl();
14
16 void print(std::ostream& stream) const;
18 std::string to_string() const;
19
21 void save(std::ostream& stream) const;
23 static std::shared_ptr<number_impl> read_library(std::istream& stream);
24
25 bool equals(number_impl const& rhs) const;
26 bool less(number_impl const& rhs) const;
27
28 std::shared_ptr<number_impl> add(number_impl const& rhs);
29 std::shared_ptr<number_impl> subtract(number_impl const& rhs);
30 std::shared_ptr<number_impl> multiply(number_impl const& rhs);
31 std::shared_ptr<number_impl> divide(number_impl const& rhs);
32
33 void add_ref();
34 void del_ref();
35
46 std::shared_ptr<number_impl> promote(number_impl& rhs) const;
47
48 virtual std::shared_ptr<number_impl> promote_to_void();
49 virtual std::shared_ptr<number_impl> promote_to_long();
50 virtual std::shared_ptr<number_impl> promote_to_rational();
51 virtual std::shared_ptr<number_impl> promote_to_double();
52
53 protected:
54 number_impl() {}
55
56 private:
57 number_impl(number_impl const& n);
58 number_impl& operator=(number_impl const& n);
59
60 virtual std::string do_to_string() const = 0;
61 virtual void do_save(std::ostream& stream) const = 0;
62
63 virtual bool do_equals(number_impl const& rhs) const = 0;
64 virtual bool do_less(number_impl const& rhs) const = 0;
65
66 virtual std::shared_ptr<number_impl> do_add(number_impl const& rhs) = 0;
67 virtual std::shared_ptr<number_impl> do_subtract(number_impl const& rhs) = 0;
68 virtual std::shared_ptr<number_impl> do_multiply(number_impl const& rhs) = 0;
69 virtual std::shared_ptr<number_impl> do_divide(number_impl const& rhs) = 0;
70
71 virtual std::shared_ptr<number_impl> do_promote(number_impl& rhs) const = 0;
72
73 std::size_t refcount_;
74 };
75
80 class number_void : public number_impl {
81 public:
82 number_void() : number_impl() {}
83 private:
84 virtual std::string do_to_string() const;
85 virtual void do_save(std::ostream& stream) const;
86 virtual bool do_equals(number_impl const& rhs) const;
87 virtual bool do_less(number_impl const& rhs) const;
88 virtual std::shared_ptr<number_impl> do_add(number_impl const& rhs);
89 virtual std::shared_ptr<number_impl> do_subtract(number_impl const& rhs);
90 virtual std::shared_ptr<number_impl> do_multiply(number_impl const& rhs);
91 virtual std::shared_ptr<number_impl> do_divide(number_impl const& rhs);
92 virtual std::shared_ptr<number_impl> do_promote(number_impl& rhs) const;
93 };
94
98 class number_long : public number_impl {
99 public:
100 number_long(long long value) : number_impl(), value_(value) {}
101 long long value() const { return value_; }
102 virtual std::shared_ptr<number_impl> promote_to_void();
103 virtual std::shared_ptr<number_impl> promote_to_rational();
104 virtual std::shared_ptr<number_impl> promote_to_double();
105 private:
106 virtual std::string do_to_string() const;
107 virtual void do_save(std::ostream& stream) const;
108 virtual bool do_equals(number_impl const& rhs) const;
109 virtual bool do_less(number_impl const& rhs) const;
110 virtual std::shared_ptr<number_impl> do_add(number_impl const& rhs);
111 virtual std::shared_ptr<number_impl> do_subtract(number_impl const& rhs);
112 virtual std::shared_ptr<number_impl> do_multiply(number_impl const& rhs);
113 virtual std::shared_ptr<number_impl> do_divide(number_impl const& rhs);
114 virtual std::shared_ptr<number_impl> do_promote(number_impl& rhs) const;
115
116 long long value_;
117 };
118
122 class number_rational: public number_impl {
123 public:
124 number_rational(rational<long long> value) : number_impl(), value_(value) {}
125 number_rational(long long num) : number_impl(), value_(num) {}
126 number_rational(long long num, long long den) : number_impl(), value_(num, den) {}
127 rational<long long> value() const { return value_; }
128 virtual std::shared_ptr<number_impl> promote_to_void();
129 virtual std::shared_ptr<number_impl> promote_to_double();
130 private:
131 virtual std::string do_to_string() const;
132 virtual void do_save(std::ostream& stream) const;
133 virtual bool do_equals(number_impl const& rhs) const;
134 virtual bool do_less(number_impl const& rhs) const;
135 virtual std::shared_ptr<number_impl> do_add(number_impl const& rhs);
136 virtual std::shared_ptr<number_impl> do_subtract(number_impl const& rhs);
137 virtual std::shared_ptr<number_impl> do_multiply(number_impl const& rhs);
138 virtual std::shared_ptr<number_impl> do_divide(number_impl const& rhs);
139 virtual std::shared_ptr<number_impl> do_promote(number_impl& rhs) const;
140
141 rational<long long> value_;
142 };
143
147 class number_double : public number_impl {
148 public:
149 number_double(double value) : number_impl(), value_(value) {}
150 double value() const { return value_; }
151 virtual std::shared_ptr<number_impl> promote_to_void();
152 private:
153 virtual std::string do_to_string() const;
154 virtual void do_save(std::ostream& stream) const;
155 virtual bool do_equals(number_impl const& rhs) const;
156 virtual bool do_less(number_impl const& rhs) const;
157 virtual std::shared_ptr<number_impl> do_add(number_impl const& rhs);
158 virtual std::shared_ptr<number_impl> do_subtract(number_impl const& rhs);
159 virtual std::shared_ptr<number_impl> do_multiply(number_impl const& rhs);
160 virtual std::shared_ptr<number_impl> do_divide(number_impl const& rhs);
161 virtual std::shared_ptr<number_impl> do_promote(number_impl& rhs) const;
162
163 double value_;
164 };
165
166 #endif
number_rational::do_less
virtual bool do_less(number_impl const &rhs) const
Definition: number_impl.cpp:265
number_double::do_save
virtual void do_save(std::ostream &stream) const
Definition: number_impl.cpp:336
number_void::do_save
virtual void do_save(std::ostream &stream) const
Definition: number_impl.cpp:125
number_impl::do_less
virtual bool do_less(number_impl const &rhs) const =0
number_void::do_multiply
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
Definition: number_impl.cpp:156
number_double::number_double
number_double(double value)
Definition: number_impl.hpp:149
number_long::value
long long value() const
Definition: number_impl.hpp:101
number_rational::do_multiply
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
Definition: number_impl.cpp:289
rational.hpp
number_impl::save
void save(std::ostream &stream) const
Definition: number_impl.cpp:12
number_double::do_add
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
Definition: number_impl.cpp:354
number_impl::del_ref
void del_ref()
number_impl::promote_to_double
virtual std::shared_ptr< number_impl > promote_to_double()
Definition: number_impl.cpp:112
number_impl::do_subtract
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)=0
number_void::do_add
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
Definition: number_impl.cpp:146
number_rational::promote_to_void
virtual std::shared_ptr< number_impl > promote_to_void()
Definition: number_impl.cpp:316
number_impl::less
bool less(number_impl const &rhs) const
Definition: number_impl.cpp:68
number_long::value_
long long value_
Definition: number_impl.hpp:116
number_impl::promote
std::shared_ptr< number_impl > promote(number_impl &rhs) const
Definition: number_impl.cpp:56
number_long::do_subtract
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
Definition: number_impl.cpp:205
number_double
Definition: number_impl.hpp:147
number_long::do_less
virtual bool do_less(number_impl const &rhs) const
Definition: number_impl.cpp:194
number_impl::~number_impl
virtual ~number_impl()
Definition: number_impl.cpp:9
number_rational::do_add
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
Definition: number_impl.cpp:271
number_impl::number_impl
number_impl()
Definition: number_impl.hpp:54
number_rational::do_equals
virtual bool do_equals(number_impl const &rhs) const
Definition: number_impl.cpp:259
number_impl::operator=
number_impl & operator=(number_impl const &n)
not implemented
number_rational::number_rational
number_rational(long long num, long long den)
Definition: number_impl.hpp:126
number_long::promote_to_double
virtual std::shared_ptr< number_impl > promote_to_double()
Definition: number_impl.cpp:239
number_double::do_subtract
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
Definition: number_impl.cpp:359
number_rational::do_divide
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
Definition: number_impl.cpp:298
number_rational::number_rational
number_rational(long long num)
Definition: number_impl.hpp:125
number_impl::print
void print(std::ostream &stream) const
Definition: number_impl.cpp:44
number_long::promote_to_rational
virtual std::shared_ptr< number_impl > promote_to_rational()
Definition: number_impl.cpp:234
number_impl::add
std::shared_ptr< number_impl > add(number_impl const &rhs)
Definition: number_impl.cpp:74
number_rational::do_save
virtual void do_save(std::ostream &stream) const
Definition: number_impl.cpp:253
number_impl::do_add
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)=0
number_rational::number_rational
number_rational(rational< long long > value)
Definition: number_impl.hpp:124
number_void::do_promote
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
Definition: number_impl.cpp:166
number_impl::do_save
virtual void do_save(std::ostream &stream) const =0
number_impl::do_promote
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const =0
number_rational
Definition: number_impl.hpp:122
number_impl::add_ref
void add_ref()
number_void::do_less
virtual bool do_less(number_impl const &rhs) const
Definition: number_impl.cpp:139
number_impl::subtract
std::shared_ptr< number_impl > subtract(number_impl const &rhs)
Definition: number_impl.cpp:79
number_double::do_promote
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
Definition: number_impl.cpp:377
rational< long long >
number_impl::do_divide
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)=0
number_rational::do_to_string
virtual std::string do_to_string() const
Definition: number_impl.cpp:245
number_void::do_subtract
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
Definition: number_impl.cpp:151
number_rational::value
rational< long long > value() const
Definition: number_impl.hpp:127
number_double::value_
double value_
Definition: number_impl.hpp:163
number_long::do_equals
virtual bool do_equals(number_impl const &rhs) const
Definition: number_impl.cpp:188
number_impl::do_to_string
virtual std::string do_to_string() const =0
number_impl::read_library
static std::shared_ptr< number_impl > read_library(std::istream &stream)
Definition: number_impl.cpp:18
number_impl::promote_to_rational
virtual std::shared_ptr< number_impl > promote_to_rational()
Definition: number_impl.cpp:107
number_void::number_void
number_void()
Definition: number_impl.hpp:82
number_impl::to_string
std::string to_string() const
Definition: number_impl.cpp:50
number_impl
Definition: number_impl.hpp:10
number_double::do_to_string
virtual std::string do_to_string() const
Definition: number_impl.cpp:328
number_double::do_multiply
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
Definition: number_impl.cpp:364
number_impl::do_multiply
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)=0
number_impl::multiply
std::shared_ptr< number_impl > multiply(number_impl const &rhs)
Definition: number_impl.cpp:84
number_impl::refcount_
std::size_t refcount_
Definition: number_impl.hpp:73
number_double::do_equals
virtual bool do_equals(number_impl const &rhs) const
Definition: number_impl.cpp:342
number_impl::promote_to_void
virtual std::shared_ptr< number_impl > promote_to_void()
Definition: number_impl.cpp:97
number_long::do_divide
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
Definition: number_impl.cpp:215
number_rational::do_promote
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
Definition: number_impl.cpp:310
number_double::promote_to_void
virtual std::shared_ptr< number_impl > promote_to_void()
Definition: number_impl.cpp:383
number_rational::do_subtract
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
Definition: number_impl.cpp:280
number_void::do_divide
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
Definition: number_impl.cpp:161
number_long::do_to_string
virtual std::string do_to_string() const
Definition: number_impl.cpp:174
number_long::do_save
virtual void do_save(std::ostream &stream) const
Definition: number_impl.cpp:182
number_double::do_less
virtual bool do_less(number_impl const &rhs) const
Definition: number_impl.cpp:348
number_long::promote_to_void
virtual std::shared_ptr< number_impl > promote_to_void()
Definition: number_impl.cpp:229
number_impl::promote_to_long
virtual std::shared_ptr< number_impl > promote_to_long()
Definition: number_impl.cpp:102
number_void::do_equals
virtual bool do_equals(number_impl const &rhs) const
Definition: number_impl.cpp:132
number_long
Definition: number_impl.hpp:98
number_impl::do_equals
virtual bool do_equals(number_impl const &rhs) const =0
number_double::value
double value() const
Definition: number_impl.hpp:150
number_long::do_add
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
Definition: number_impl.cpp:200
number_void::do_to_string
virtual std::string do_to_string() const
Definition: number_impl.cpp:119
number_long::do_promote
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
Definition: number_impl.cpp:223
number_impl::equals
bool equals(number_impl const &rhs) const
Definition: number_impl.cpp:62
number_rational::value_
rational< long long > value_
Definition: number_impl.hpp:141
number_impl::divide
std::shared_ptr< number_impl > divide(number_impl const &rhs)
Definition: number_impl.cpp:89
number_rational::promote_to_double
virtual std::shared_ptr< number_impl > promote_to_double()
Definition: number_impl.cpp:321
number_long::do_multiply
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
Definition: number_impl.cpp:210
number_double::do_divide
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
Definition: number_impl.cpp:369
number_void
Definition: number_impl.hpp:80
number_long::number_long
number_long(long long value)
Definition: number_impl.hpp:100