Calculator  Step 6
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 
8 : pimpl_{std::make_shared<number_void>()}
9 {}
10 
12 : pimpl_{std::make_shared<number_long>(x)}
13 {}
14 
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 
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)
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 
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 
65 {
66  coerce(b);
67  return pimpl_->equals(*b.pimpl_);
68 }
69 
71 {
72  coerce(b);
73  return pimpl_->less(*b.pimpl_);
74 }
75 
77 {
78  coerce(rhs);
79  return number(pimpl_->add(*rhs.pimpl_));
80 }
81 
83 {
84  coerce(rhs);
85  return number(pimpl_->subtract(*rhs.pimpl_));
86 }
87 
89 {
90  coerce(rhs);
91  return number(pimpl_->multiply(*rhs.pimpl_));
92 }
93 
95 {
96  coerce(rhs);
97  return number(pimpl_->divide(*rhs.pimpl_));
98 }
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number operator-(number rhs)
Definition: number.cpp:82
std::string to_string() const
Definition: number.cpp:45
number operator+(number rhs)
Definition: number.cpp:76
number()
Definition: number.cpp:7
void print(std::ostream &stream)
Definition: number.cpp:51
bool less(number b)
Definition: number.cpp:70
number operator*(number rhs)
Definition: number.cpp:88
void save(std::ostream &stream) const
Definition: number.cpp:39
void coerce(number &rhs)
Definition: number.cpp:56
static std::shared_ptr< number_impl > read_library(std::istream &stream)
Definition: number_impl.cpp:18
bool equals(number b)
Definition: number.cpp:64
number operator/(number rhs)
Definition: number.cpp:94