Calculator Step 6

- Main Page
- Classes
- Files

- File List
- File Members

number.cpp
Go to the documentation of this file.
1 #include "number.hpp"
2 #include "number_impl.hpp"
3
4 #include <iostream>
5 #include <ostream>
6
7 number::number()
8 : pimpl_{std::make_shared<number_void>()}
9 {}
10
11 number::number(int x)
12 : pimpl_{std::make_shared<number_long>(x)}
13 {}
14
15 number::number(long x)
16 : pimpl_{std::make_shared<number_long>(x)}
17 {}
18
19 number::number(long long x)
20 : pimpl_{std::make_shared<number_long>(x)}
21 {}
22
23 number::number(rational<long long> const& x)
24 : pimpl_{std::make_shared<number_rational>(x)}
25 {}
26
27 number::number(double x)
28 : pimpl_{std::make_shared<number_double>(x)}
29 {}
30
31 number::number(std::istream& stream)
32 : pimpl_{number_impl::read_library(stream)}
33 {}
34
35 number::number(std::shared_ptr<number_impl> pimpl)
36 : pimpl_{std::move(pimpl)}
37 {}
38
39 void number::save(std::ostream& stream)
40 const
41 {
42 pimpl_->save(stream);
43 }
44
45 std::string number::to_string()
46 const
47 {
48 return pimpl_->to_string();
49 }
50
51 void number::print(std::ostream& stream)
52 {
53 pimpl_->print(stream);
54 }
55
56 void number::coerce(number& that)
57 {
58 // Promote that number to the type of this.
59 that = number(this->pimpl_->promote(*that.pimpl_));
60 // Promote this to the type of that.
61 *this = number(that.pimpl_->promote(*this->pimpl_));
62 }
63
64 bool number::equals(number b)
65 {
66 coerce(b);
67 return pimpl_->equals(*b.pimpl_);
68 }
69
70 bool number::less(number b)
71 {
72 coerce(b);
73 return pimpl_->less(*b.pimpl_);
74 }
75
76 number number::operator+(number rhs)
77 {
78 coerce(rhs);
79 return number(pimpl_->add(*rhs.pimpl_));
80 }
81
82 number number::operator-(number rhs)
83 {
84 coerce(rhs);
85 return number(pimpl_->subtract(*rhs.pimpl_));
86 }
87
88 number number::operator*(number rhs)
89 {
90 coerce(rhs);
91 return number(pimpl_->multiply(*rhs.pimpl_));
92 }
93
94 number number::operator/(number rhs)
95 {
96 coerce(rhs);
97 return number(pimpl_->divide(*rhs.pimpl_));
98 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::operator-
number operator-(number rhs)
Definition: number.cpp:82
number::to_string
std::string to_string() const
Definition: number.cpp:45
number::operator+
number operator+(number rhs)
Definition: number.cpp:76
number
Definition: number.hpp:16
number::number
number()
Definition: number.cpp:7
number::print
void print(std::ostream &stream)
Definition: number.cpp:51
number::less
bool less(number b)
Definition: number.cpp:70
number_impl.hpp
number::operator*
number operator*(number rhs)
Definition: number.cpp:88
rational< long long >
number::save
void save(std::ostream &stream) const
Definition: number.cpp:39
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56
number_impl::read_library
static std::shared_ptr< number_impl > read_library(std::istream &stream)
Definition: number_impl.cpp:18
number::equals
bool equals(number b)
Definition: number.cpp:64
number.hpp
number::operator/
number operator/(number rhs)
Definition: number.cpp:94