Calculator  Step 6
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
number_impl Class Referenceabstract

#include <number_impl.hpp>

Inheritance diagram for number_impl:
number_double number_long number_rational number_void

Public Member Functions

virtual ~number_impl ()
 
void print (std::ostream &stream) const
 
std::string to_string () const
 
void save (std::ostream &stream) const
 
bool equals (number_impl const &rhs) const
 
bool less (number_impl const &rhs) const
 
std::shared_ptr< number_impladd (number_impl const &rhs)
 
std::shared_ptr< number_implsubtract (number_impl const &rhs)
 
std::shared_ptr< number_implmultiply (number_impl const &rhs)
 
std::shared_ptr< number_impldivide (number_impl const &rhs)
 
void add_ref ()
 
void del_ref ()
 
std::shared_ptr< number_implpromote (number_impl &rhs) const
 
virtual std::shared_ptr
< number_impl
promote_to_void ()
 
virtual std::shared_ptr
< number_impl
promote_to_long ()
 
virtual std::shared_ptr
< number_impl
promote_to_rational ()
 
virtual std::shared_ptr
< number_impl
promote_to_double ()
 

Static Public Member Functions

static std::shared_ptr
< number_impl
read_library (std::istream &stream)
 

Protected Member Functions

 number_impl ()
 

Private Member Functions

 number_impl (number_impl const &n)
 not implemented More...
 
number_imploperator= (number_impl const &n)
 not implemented More...
 
virtual std::string do_to_string () const =0
 
virtual void do_save (std::ostream &stream) const =0
 
virtual bool do_equals (number_impl const &rhs) const =0
 
virtual bool do_less (number_impl const &rhs) const =0
 
virtual std::shared_ptr
< number_impl
do_add (number_impl const &rhs)=0
 
virtual std::shared_ptr
< number_impl
do_subtract (number_impl const &rhs)=0
 
virtual std::shared_ptr
< number_impl
do_multiply (number_impl const &rhs)=0
 
virtual std::shared_ptr
< number_impl
do_divide (number_impl const &rhs)=0
 
virtual std::shared_ptr
< number_impl
do_promote (number_impl &rhs) const =0
 

Private Attributes

std::size_t refcount_
 

Detailed Description

Definition at line 10 of file number_impl.hpp.

Constructor & Destructor Documentation

number_impl::~number_impl ( )
virtual

Definition at line 9 of file number_impl.cpp.

10 {}
number_impl::number_impl ( )
inlineprotected

Definition at line 54 of file number_impl.hpp.

54 {}
number_impl::number_impl ( number_impl const &  n)
private

not implemented

Member Function Documentation

std::shared_ptr< number_impl > number_impl::add ( number_impl const &  rhs)

Definition at line 74 of file number_impl.cpp.

References do_add().

75 {
76  return do_add(rhs);
77 }
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)=0
void number_impl::add_ref ( )
void number_impl::del_ref ( )
std::shared_ptr< number_impl > number_impl::divide ( number_impl const &  rhs)

Definition at line 89 of file number_impl.cpp.

References do_divide().

90 {
91  return do_divide(rhs);
92 }
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)=0
virtual std::shared_ptr<number_impl> number_impl::do_add ( number_impl const &  rhs)
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by add().

virtual std::shared_ptr<number_impl> number_impl::do_divide ( number_impl const &  rhs)
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by divide().

virtual bool number_impl::do_equals ( number_impl const &  rhs) const
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by equals().

virtual bool number_impl::do_less ( number_impl const &  rhs) const
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by less().

virtual std::shared_ptr<number_impl> number_impl::do_multiply ( number_impl const &  rhs)
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by multiply().

virtual std::shared_ptr<number_impl> number_impl::do_promote ( number_impl rhs) const
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by promote().

virtual void number_impl::do_save ( std::ostream &  stream) const
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by save().

virtual std::shared_ptr<number_impl> number_impl::do_subtract ( number_impl const &  rhs)
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by subtract().

virtual std::string number_impl::do_to_string ( ) const
privatepure virtual

Implemented in number_double, number_rational, number_long, and number_void.

Referenced by to_string().

bool number_impl::equals ( number_impl const &  rhs) const

Definition at line 62 of file number_impl.cpp.

References do_equals().

64 {
65  return do_equals(rhs);
66 }
virtual bool do_equals(number_impl const &rhs) const =0
bool number_impl::less ( number_impl const &  rhs) const

Definition at line 68 of file number_impl.cpp.

References do_less().

70 {
71  return do_less(rhs);
72 }
virtual bool do_less(number_impl const &rhs) const =0
std::shared_ptr< number_impl > number_impl::multiply ( number_impl const &  rhs)

Definition at line 84 of file number_impl.cpp.

References do_multiply().

85 {
86  return do_multiply(rhs);
87 }
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)=0
number_impl& number_impl::operator= ( number_impl const &  n)
private

not implemented

void number_impl::print ( std::ostream &  stream) const

Print in a human-readable form.

Definition at line 44 of file number_impl.cpp.

References to_string().

46 {
47  stream << to_string();
48 }
std::string to_string() const
Definition: number_impl.cpp:50
std::shared_ptr< number_impl > number_impl::promote ( number_impl rhs) const

Promote rhs to match the type of this number. This function uses double-dispatch, so this number calls a virtual promote_to_*() function that notifies rhs of the desired type. If the desired type is "larger" than the type of rhs, the promote_to_*() function returns a new number_impl of the desired type. Otherwise, the function returns rhs.

Parameters
rhsThe right-hand operand of an arithmetic expression.

Definition at line 56 of file number_impl.cpp.

References do_promote().

58 {
59  return do_promote(rhs);
60 }
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const =0
std::shared_ptr< number_impl > number_impl::promote_to_double ( )
virtual

Reimplemented in number_rational, and number_long.

Definition at line 112 of file number_impl.cpp.

Referenced by number_double::do_promote().

113 {
114  return shared_from_this();
115 }
std::shared_ptr< number_impl > number_impl::promote_to_long ( )
virtual

Definition at line 102 of file number_impl.cpp.

Referenced by number_long::do_promote().

103 {
104  return shared_from_this();
105 }
std::shared_ptr< number_impl > number_impl::promote_to_rational ( )
virtual

Reimplemented in number_long.

Definition at line 107 of file number_impl.cpp.

Referenced by number_rational::do_promote().

108 {
109  return shared_from_this();
110 }
std::shared_ptr< number_impl > number_impl::promote_to_void ( )
virtual

Reimplemented in number_double, number_rational, and number_long.

Definition at line 97 of file number_impl.cpp.

Referenced by number_void::do_promote().

98 {
99  return shared_from_this();
100 }
std::shared_ptr< number_impl > number_impl::read_library ( std::istream &  stream)
static

Read the number type from a library file, then create the proper kind of number.

Definition at line 18 of file number_impl.cpp.

Referenced by number::number().

19 {
20  std::string type{};
21  if (not (stream >> type))
22  throw calc_error{"malformed library, missing number type"};
23 
24  if (type == "void")
25  return std::make_shared<number_void>();
26 
27  if (type == "long") {
28  long long x{};
29  if (not (stream >> x))
30  throw calc_error{"malformed library, missing long value"};
31  return std::make_shared<number_long>(x);
32  }
33 
34  if (type == "double") {
35  double x{};
36  if (not (stream >> x))
37  throw calc_error{"malformed library, missing double value"};
38  return std::make_shared<number_double>(x);
39  }
40 
41  throw calc_error{"malformed library, unknown number type: " + type};
42 }
void number_impl::save ( std::ostream &  stream) const

Save a number to a library file.

Definition at line 12 of file number_impl.cpp.

References do_save().

14 {
15  do_save(stream);
16 }
virtual void do_save(std::ostream &stream) const =0
std::shared_ptr< number_impl > number_impl::subtract ( number_impl const &  rhs)

Definition at line 79 of file number_impl.cpp.

References do_subtract().

80 {
81  return do_subtract(rhs);
82 }
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)=0
std::string number_impl::to_string ( ) const

Convert to a string.

Definition at line 50 of file number_impl.cpp.

References do_to_string().

Referenced by print().

52 {
53  return do_to_string();
54 }
virtual std::string do_to_string() const =0

Member Data Documentation

std::size_t number_impl::refcount_
private

Definition at line 73 of file number_impl.hpp.


The documentation for this class was generated from the following files: