using smart pointers with delete built in

This commit is contained in:
vlogozzo 2019-03-23 21:29:18 -04:00
commit 2d4f57f6dd

View file

@ -31,22 +31,18 @@ namespace CalcEngine
Rational::Rational(int32_t i) Rational::Rational(int32_t i)
{ {
PRAT pr = longtorat(static_cast<long>(i)); unique_ptr<RAT, decltype(&_destroyrat)> pr(Ulongtorat(static_cast<unsigned long>(i)), &_destroyrat);
m_p = Number{ pr->pp }; m_p = Number{ pr->pp };
m_q = Number{ pr->pq }; m_q = Number{ pr->pq };
destroyrat(pr);
} }
Rational::Rational(uint32_t ui) Rational::Rational(uint32_t ui)
{ {
PRAT pr = Ulongtorat(static_cast<unsigned long>(ui)); unique_ptr<RAT, decltype(&_destroyrat)> pr(Ulongtorat(static_cast<unsigned long>(ui)), &_destroyrat);
m_p = Number{ pr->pp }; m_p = Number{ pr->pp };
m_q = Number{ pr->pq }; m_q = Number{ pr->pq };
destroyrat(pr);
} }
Rational::Rational(uint64_t ui) Rational::Rational(uint64_t ui)
@ -92,228 +88,198 @@ namespace CalcEngine
Rational& Rational::operator+=(Rational const& rhs) Rational& Rational::operator+=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
addrat(&lhsRat, rhsRat, RATIONAL_PRECISION); addrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator-=(Rational const& rhs) Rational& Rational::operator-=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
subrat(&lhsRat, rhsRat, RATIONAL_PRECISION); subrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator*=(Rational const& rhs) Rational& Rational::operator*=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
mulrat(&lhsRat, rhsRat, RATIONAL_PRECISION); mulrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator/=(Rational const& rhs) Rational& Rational::operator/=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
divrat(&lhsRat, rhsRat, RATIONAL_PRECISION); divrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator%=(Rational const& rhs) Rational& Rational::operator%=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
modrat(&lhsRat, rhsRat); modrat(&lhsPrat, rhsSmartRat.get());
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator<<=(Rational const& rhs) Rational& Rational::operator<<=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
lshrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION); lshrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_BASE, RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator>>=(Rational const& rhs) Rational& Rational::operator>>=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
rshrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION); rshrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_BASE, RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator&=(Rational const& rhs) Rational& Rational::operator&=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
andrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION); andrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_BASE, RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator|=(Rational const& rhs) Rational& Rational::operator|=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
orrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION); orrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_BASE, RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
Rational& Rational::operator^=(Rational const& rhs) Rational& Rational::operator^=(Rational const& rhs)
{ {
PRAT lhsRat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(this->ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); auto lhsPrat = lhsSmartRat.get();
unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
try try
{ {
xorrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION); xorrat(&lhsPrat, rhsSmartRat.get(), RATIONAL_BASE, RATIONAL_PRECISION);
destroyrat(rhsRat);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
*this = Rational{ lhsRat }; *this = Rational{ lhsSmartRat.get() };
destroyrat(lhsRat);
return *this; return *this;
} }
@ -380,24 +346,19 @@ namespace CalcEngine
bool operator==(Rational const& lhs, Rational const& rhs) bool operator==(Rational const& lhs, Rational const& rhs)
{ {
PRAT lhsRat = lhs.ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(lhs.ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
bool result = false; bool result = false;
try try
{ {
result = rat_equ(lhsRat, rhsRat, RATIONAL_PRECISION); result = rat_equ(lhsSmartRat.get(), rhsSmartRat.get(), RATIONAL_PRECISION);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
destroyrat(lhsRat);
destroyrat(rhsRat);
return result; return result;
} }
@ -408,24 +369,19 @@ namespace CalcEngine
bool operator<(Rational const& lhs, Rational const& rhs) bool operator<(Rational const& lhs, Rational const& rhs)
{ {
PRAT lhsRat = lhs.ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> lhsSmartRat(lhs.ToPRAT(), &_destroyrat);
PRAT rhsRat = rhs.ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> rhsSmartRat(rhs.ToPRAT(), &_destroyrat);
bool result = false; bool result = false;
try try
{ {
result = rat_lt(lhsRat, rhsRat, RATIONAL_PRECISION); result = rat_lt(lhsSmartRat.get(), rhsSmartRat.get(), RATIONAL_PRECISION);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error); throw(error);
} }
destroyrat(lhsRat);
destroyrat(rhsRat);
return result; return result;
} }
@ -446,40 +402,35 @@ namespace CalcEngine
wstring Rational::ToString(uint32_t radix, NUMOBJ_FMT fmt, int32_t precision) const wstring Rational::ToString(uint32_t radix, NUMOBJ_FMT fmt, int32_t precision) const
{ {
PRAT rat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> SmartRat(this->ToPRAT(), &_destroyrat);
auto PRAT = SmartRat.get();
wstring result{}; wstring result{};
try try
{ {
result = RatToString(rat, fmt, radix, precision); result = RatToString(PRAT, fmt, radix, precision);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(rat);
throw(error); throw(error);
} }
destroyrat(rat);
return result; return result;
} }
uint64_t Rational::ToUInt64_t() const uint64_t Rational::ToUInt64_t() const
{ {
PRAT rat = this->ToPRAT(); unique_ptr<RAT, decltype(&_destroyrat)> smartRat(this->ToPRAT(), &_destroyrat);
uint64_t result; uint64_t result;
try try
{ {
result = rattoUlonglong(rat, RATIONAL_BASE, RATIONAL_PRECISION); result = rattoUlonglong(smartRat.get(), RATIONAL_BASE, RATIONAL_PRECISION);
} }
catch (DWORD error) catch (DWORD error)
{ {
destroyrat(rat);
throw(error); throw(error);
} }
destroyrat(rat);
return result; return result;
} }
} }