From db4a6eb9ea5dbd213eb75b28d6eed4c9464af5ae Mon Sep 17 00:00:00 2001 From: Josh Koon <45607479+joshkoon@users.noreply.github.com> Date: Mon, 28 Jan 2019 19:34:36 -0800 Subject: [PATCH] Return to initialization pattern in ExpressionCommand --- src/CalcManager/CEngine/History.cpp | 7 ++++--- src/CalcManager/ExpressionCommand.cpp | 28 +++++++++++++++++--------- src/CalcManager/ExpressionCommand.h | 5 +++-- src/CalcManager/Header Files/History.h | 2 +- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/CalcManager/CEngine/History.cpp b/src/CalcManager/CEngine/History.cpp index b2ba1c1..95347a5 100644 --- a/src/CalcManager/CEngine/History.cpp +++ b/src/CalcManager/CEngine/History.cpp @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pch.h" @@ -52,7 +52,7 @@ CHistoryCollector::~CHistoryCollector() } } -void CHistoryCollector::AddOpndToHistory(wstring_view numStr, Rational rat, bool fRepetition) +void CHistoryCollector::AddOpndToHistory(wstring_view numStr, Rational const& rat, bool fRepetition) { std::shared_ptr> commands = std::make_shared>(); // Check for negate @@ -92,7 +92,8 @@ void CHistoryCollector::AddOpndToHistory(wstring_view numStr, Rational rat, bool } } - auto operandCommand = std::make_shared(commands, rat, fNegative, fDecimal, fSciFmt); + auto operandCommand = std::make_shared(commands, fNegative, fDecimal, fSciFmt); + operandCommand->Initialize(rat); int iCommandEnd = AddCommand(operandCommand); m_lastOpStartIndex = IchAddSzToEquationSz(numStr, iCommandEnd); diff --git a/src/CalcManager/ExpressionCommand.cpp b/src/CalcManager/ExpressionCommand.cpp index c876617..07c4cf7 100644 --- a/src/CalcManager/ExpressionCommand.cpp +++ b/src/CalcManager/ExpressionCommand.cpp @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "pch.h" @@ -96,13 +96,18 @@ void CBinaryCommand::Accept(_In_ ISerializeCommandVisitor &commandVisitor) } COpndCommand::COpndCommand(_In_ shared_ptr> const &commands, - Rational const& rat, - bool fNegative, - bool fDecimal, - bool fSciFmt) : - m_commands(commands), m_value{ rat }, m_fNegative(fNegative), m_fDecimal(fDecimal), m_fSciFmt(fSciFmt) + bool fNegative, + bool fDecimal, + bool fSciFmt) : + m_commands(commands), m_fNegative(fNegative), m_fDecimal(fDecimal), m_fSciFmt(fSciFmt), m_fInitialized(false), m_value{} {} +void COpndCommand::Initialize(Rational const& rat) +{ + m_value = rat; + m_fInitialized = true; +} + const shared_ptr> & COpndCommand::GetCommands() const { return m_commands; @@ -282,9 +287,14 @@ const wstring & COpndCommand::GetToken(wchar_t decimalSymbol) wstring COpndCommand::GetString(uint32_t radix, int32_t precision, wchar_t decimalSymbol) { - PRAT valRat = m_value.ToPRAT(); - auto result = NumObjToString(valRat, radix, eNUMOBJ_FMT::FMT_FLOAT, precision); - destroyrat(valRat); + wstring result{}; + + if (m_fInitialized) + { + PRAT valRat = m_value.ToPRAT(); + result = NumObjToString(valRat, radix, eNUMOBJ_FMT::FMT_FLOAT, precision); + destroyrat(valRat); + } return result; } diff --git a/src/CalcManager/ExpressionCommand.h b/src/CalcManager/ExpressionCommand.h index ffdb043..10bed35 100644 --- a/src/CalcManager/ExpressionCommand.h +++ b/src/CalcManager/ExpressionCommand.h @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once @@ -50,11 +50,11 @@ class COpndCommand : public IOpndCommand { public: COpndCommand(_In_ std::shared_ptr> const &commands, - CalcEngine::Rational const& rat, bool fNegative, bool fDecimal, bool fSciFmt); ~COpndCommand(); + void Initialize(CalcEngine::Rational const& rat); const std::shared_ptr> & GetCommands() const; void SetCommands(std::shared_ptr> const& commands); @@ -74,6 +74,7 @@ private: bool m_fNegative; bool m_fSciFmt; bool m_fDecimal; + bool m_fInitialized; std::wstring m_token; CalcEngine::Rational m_value; void ClearAllAndAppendCommand(CalculationManager::Command command); diff --git a/src/CalcManager/Header Files/History.h b/src/CalcManager/Header Files/History.h index b084375..5deb5ff 100644 --- a/src/CalcManager/Header Files/History.h +++ b/src/CalcManager/Header Files/History.h @@ -17,7 +17,7 @@ class CHistoryCollector { public: CHistoryCollector(ICalcDisplay *pCalcDisplay, std::shared_ptr pHistoryDisplay, wchar_t decimalSymbol); // Can throw errors ~CHistoryCollector(); - void AddOpndToHistory(std::wstring_view numStr, CalcEngine::Rational rat, bool fRepetition = false); + void AddOpndToHistory(std::wstring_view numStr, CalcEngine::Rational const& rat, bool fRepetition = false); void RemoveLastOpndFromHistory(); void AddBinOpToHistory(int nOpCode, bool fNoRepetition = true); void ChangeLastBinOp(int nOpCode, bool fPrecInvToHigher);