Remove saved commands from CalculatorManager (#1230)
This commit is contained in:
parent
79bd149b12
commit
c37f540265
@ -10,16 +10,11 @@ using namespace std;
|
|||||||
using namespace CalcEngine;
|
using namespace CalcEngine;
|
||||||
|
|
||||||
static constexpr size_t MAX_HISTORY_ITEMS = 20;
|
static constexpr size_t MAX_HISTORY_ITEMS = 20;
|
||||||
static constexpr size_t SERIALIZED_NUMBER_MINSIZE = 3;
|
|
||||||
|
|
||||||
#ifndef _MSC_VER
|
#ifndef _MSC_VER
|
||||||
#define __pragma(x)
|
#define __pragma(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Converts Memory Command enum value to unsigned char,
|
|
||||||
// while ignoring Warning C4309: 'conversion' : truncation of constant value
|
|
||||||
#define MEMORY_COMMAND_TO_UNSIGNED_CHAR(c) __pragma(warning(push)) __pragma(warning(disable : 4309)) static_cast<unsigned char>(c) __pragma(warning(pop))
|
|
||||||
|
|
||||||
namespace CalculationManager
|
namespace CalculationManager
|
||||||
{
|
{
|
||||||
CalculatorManager::CalculatorManager(_In_ ICalcDisplay* displayCallback, _In_ IResourceProvider* resourceProvider)
|
CalculatorManager::CalculatorManager(_In_ ICalcDisplay* displayCallback, _In_ IResourceProvider* resourceProvider)
|
||||||
@ -30,7 +25,6 @@ namespace CalculationManager
|
|||||||
, m_persistedPrimaryValue()
|
, m_persistedPrimaryValue()
|
||||||
, m_isExponentialFormat(false)
|
, m_isExponentialFormat(false)
|
||||||
, m_currentDegreeMode(Command::CommandNULL)
|
, m_currentDegreeMode(Command::CommandNULL)
|
||||||
, m_savedDegreeMode(Command::CommandDEG)
|
|
||||||
, m_pStdHistory(new CalculatorHistory(MAX_HISTORY_ITEMS))
|
, m_pStdHistory(new CalculatorHistory(MAX_HISTORY_ITEMS))
|
||||||
, m_pSciHistory(new CalculatorHistory(MAX_HISTORY_ITEMS))
|
, m_pSciHistory(new CalculatorHistory(MAX_HISTORY_ITEMS))
|
||||||
, m_pHistory(nullptr)
|
, m_pHistory(nullptr)
|
||||||
@ -132,7 +126,6 @@ namespace CalculationManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void CalculatorManager::Reset(bool clearMemory /* = true*/)
|
void CalculatorManager::Reset(bool clearMemory /* = true*/)
|
||||||
{
|
{
|
||||||
m_savedCommands.clear();
|
|
||||||
SetStandardMode();
|
SetStandardMode();
|
||||||
|
|
||||||
if (m_scientificCalculatorEngine)
|
if (m_scientificCalculatorEngine)
|
||||||
@ -239,13 +232,6 @@ namespace CalculationManager
|
|||||||
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(command));
|
m_currentCalculatorEngine->ProcessCommand(static_cast<OpCode>(command));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_savedCommands.clear(); // Clear the previous command history
|
|
||||||
|
|
||||||
if (command != Command::CommandEQU && command != Command::CommandCLEAR)
|
|
||||||
{
|
|
||||||
m_savedCommands.push_back(MapCommandForSerialize(command));
|
|
||||||
}
|
|
||||||
m_savedDegreeMode = m_currentDegreeMode;
|
|
||||||
InputChanged();
|
InputChanged();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -255,11 +241,6 @@ namespace CalculationManager
|
|||||||
m_currentDegreeMode = command;
|
m_currentDegreeMode = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command != Command::CommandFE)
|
|
||||||
{
|
|
||||||
m_savedCommands.push_back(MapCommandForSerialize(command)); // Save the commands in the m_savedCommands
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (command)
|
switch (command)
|
||||||
{
|
{
|
||||||
case Command::CommandASIN:
|
case Command::CommandASIN:
|
||||||
@ -325,37 +306,6 @@ namespace CalculationManager
|
|||||||
InputChanged();
|
InputChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert Command to unsigned char.
|
|
||||||
/// Since some Commands are higher than 255, they are saved after subtracting 255
|
|
||||||
/// The smallest Command is CommandSIGN = 80, thus, subtracted value does not overlap with other values.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="command">Enum Command</command>
|
|
||||||
unsigned char CalculatorManager::MapCommandForSerialize(Command command)
|
|
||||||
{
|
|
||||||
unsigned int commandToSave = static_cast<unsigned int>(command);
|
|
||||||
if (commandToSave > UCHAR_MAX)
|
|
||||||
{
|
|
||||||
commandToSave -= UCHAR_MAX;
|
|
||||||
}
|
|
||||||
return static_cast<unsigned char>(commandToSave);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert Command to unsigned int
|
|
||||||
/// The command that is smaller than 80, CommandSIGN, can be converted back to original value by adding 255.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="command">unsigned char value represent the saved command</command>
|
|
||||||
unsigned int CalculatorManager::MapCommandForDeSerialize(unsigned char command)
|
|
||||||
{
|
|
||||||
unsigned int commandToLoad = command;
|
|
||||||
if (command < static_cast<unsigned int>(Command::CommandSIGN))
|
|
||||||
{
|
|
||||||
commandToLoad += UCHAR_MAX;
|
|
||||||
}
|
|
||||||
return commandToLoad;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Load the persisted value that is saved in memory of CalcEngine
|
/// Load the persisted value that is saved in memory of CalcEngine
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -372,8 +322,6 @@ namespace CalculationManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void CalculatorManager::MemorizeNumber()
|
void CalculatorManager::MemorizeNumber()
|
||||||
{
|
{
|
||||||
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizeNumber));
|
|
||||||
|
|
||||||
if (m_currentCalculatorEngine->FInErrorState())
|
if (m_currentCalculatorEngine->FInErrorState())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -401,8 +349,6 @@ namespace CalculationManager
|
|||||||
/// <param name="indexOfMemory">Index of the target memory</param>
|
/// <param name="indexOfMemory">Index of the target memory</param>
|
||||||
void CalculatorManager::MemorizedNumberLoad(_In_ unsigned int indexOfMemory)
|
void CalculatorManager::MemorizedNumberLoad(_In_ unsigned int indexOfMemory)
|
||||||
{
|
{
|
||||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberLoad, indexOfMemory);
|
|
||||||
|
|
||||||
if (m_currentCalculatorEngine->FInErrorState())
|
if (m_currentCalculatorEngine->FInErrorState())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -421,8 +367,6 @@ namespace CalculationManager
|
|||||||
/// <param name="indexOfMemory">Index of the target memory</param>
|
/// <param name="indexOfMemory">Index of the target memory</param>
|
||||||
void CalculatorManager::MemorizedNumberAdd(_In_ unsigned int indexOfMemory)
|
void CalculatorManager::MemorizedNumberAdd(_In_ unsigned int indexOfMemory)
|
||||||
{
|
{
|
||||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberAdd, indexOfMemory);
|
|
||||||
|
|
||||||
if (m_currentCalculatorEngine->FInErrorState())
|
if (m_currentCalculatorEngine->FInErrorState())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -449,7 +393,6 @@ namespace CalculationManager
|
|||||||
{
|
{
|
||||||
if (indexOfMemory < m_memorizedNumbers.size())
|
if (indexOfMemory < m_memorizedNumbers.size())
|
||||||
{
|
{
|
||||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberClear, indexOfMemory);
|
|
||||||
m_memorizedNumbers.erase(m_memorizedNumbers.begin() + indexOfMemory);
|
m_memorizedNumbers.erase(m_memorizedNumbers.begin() + indexOfMemory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -462,8 +405,6 @@ namespace CalculationManager
|
|||||||
/// <param name="indexOfMemory">Index of the target memory</param>
|
/// <param name="indexOfMemory">Index of the target memory</param>
|
||||||
void CalculatorManager::MemorizedNumberSubtract(_In_ unsigned int indexOfMemory)
|
void CalculatorManager::MemorizedNumberSubtract(_In_ unsigned int indexOfMemory)
|
||||||
{
|
{
|
||||||
SaveMemoryCommand(MemoryCommand::MemorizedNumberSubtract, indexOfMemory);
|
|
||||||
|
|
||||||
if (m_currentCalculatorEngine->FInErrorState())
|
if (m_currentCalculatorEngine->FInErrorState())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@ -495,7 +436,6 @@ namespace CalculationManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void CalculatorManager::MemorizedNumberClearAll()
|
void CalculatorManager::MemorizedNumberClearAll()
|
||||||
{
|
{
|
||||||
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizedNumberClearAll));
|
|
||||||
m_memorizedNumbers.clear();
|
m_memorizedNumbers.clear();
|
||||||
|
|
||||||
m_currentCalculatorEngine->ProcessCommand(IDC_MCLEAR);
|
m_currentCalculatorEngine->ProcessCommand(IDC_MCLEAR);
|
||||||
@ -537,16 +477,6 @@ namespace CalculationManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalculatorManager::SaveMemoryCommand(_In_ MemoryCommand command, _In_ unsigned int indexOfMemory)
|
|
||||||
{
|
|
||||||
m_savedCommands.push_back(MEMORY_COMMAND_TO_UNSIGNED_CHAR(command));
|
|
||||||
if (indexOfMemory > UCHAR_MAX)
|
|
||||||
{
|
|
||||||
throw invalid_argument("Unexpected value. IndexOfMemory is bigger than the biggest unsigned char");
|
|
||||||
}
|
|
||||||
m_savedCommands.push_back(static_cast<unsigned char>(indexOfMemory));
|
|
||||||
}
|
|
||||||
|
|
||||||
vector<shared_ptr<HISTORYITEM>> const& CalculatorManager::GetHistoryItems()
|
vector<shared_ptr<HISTORYITEM>> const& CalculatorManager::GetHistoryItems()
|
||||||
{
|
{
|
||||||
return m_pHistory->GetHistory();
|
return m_pHistory->GetHistory();
|
||||||
|
@ -44,6 +44,7 @@ namespace CalculationManager
|
|||||||
class CalculatorManager final : public ICalcDisplay
|
class CalculatorManager final : public ICalcDisplay
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
static const unsigned int m_maximumMemorySize = 100;
|
||||||
ICalcDisplay* const m_displayCallback;
|
ICalcDisplay* const m_displayCallback;
|
||||||
CCalcEngine* m_currentCalculatorEngine;
|
CCalcEngine* m_currentCalculatorEngine;
|
||||||
std::unique_ptr<CCalcEngine> m_scientificCalculatorEngine;
|
std::unique_ptr<CCalcEngine> m_scientificCalculatorEngine;
|
||||||
@ -54,21 +55,8 @@ namespace CalculationManager
|
|||||||
|
|
||||||
std::vector<CalcEngine::Rational> m_memorizedNumbers;
|
std::vector<CalcEngine::Rational> m_memorizedNumbers;
|
||||||
CalcEngine::Rational m_persistedPrimaryValue;
|
CalcEngine::Rational m_persistedPrimaryValue;
|
||||||
|
|
||||||
bool m_isExponentialFormat;
|
bool m_isExponentialFormat;
|
||||||
|
|
||||||
static const unsigned int m_maximumMemorySize = 100;
|
|
||||||
|
|
||||||
// For persistence
|
|
||||||
std::vector<unsigned char> m_savedCommands;
|
|
||||||
std::vector<long> m_savedPrimaryValue;
|
|
||||||
std::vector<long> m_currentSerializedMemory;
|
|
||||||
Command m_currentDegreeMode;
|
Command m_currentDegreeMode;
|
||||||
Command m_savedDegreeMode;
|
|
||||||
unsigned char MapCommandForSerialize(Command command);
|
|
||||||
unsigned int MapCommandForDeSerialize(unsigned char command);
|
|
||||||
|
|
||||||
void SaveMemoryCommand(_In_ MemoryCommand command, _In_ unsigned int indexOfMemory);
|
|
||||||
|
|
||||||
void MemorizedNumberSelect(_In_ unsigned int);
|
void MemorizedNumberSelect(_In_ unsigned int);
|
||||||
void MemorizedNumberChanged(_In_ unsigned int);
|
void MemorizedNumberChanged(_In_ unsigned int);
|
||||||
@ -112,10 +100,6 @@ namespace CalculationManager
|
|||||||
|
|
||||||
bool IsEngineRecording();
|
bool IsEngineRecording();
|
||||||
bool IsInputEmpty();
|
bool IsInputEmpty();
|
||||||
const std::vector<unsigned char>& GetSavedCommands() const
|
|
||||||
{
|
|
||||||
return m_savedCommands;
|
|
||||||
}
|
|
||||||
void SetRadix(RadixType iRadixType);
|
void SetRadix(RadixType iRadixType);
|
||||||
void SetMemorizedNumbersString();
|
void SetMemorizedNumbersString();
|
||||||
std::wstring GetResultForRadix(uint32_t radix, int32_t precision, bool groupDigitsPerRadix);
|
std::wstring GetResultForRadix(uint32_t radix, int32_t precision, bool groupDigitsPerRadix);
|
||||||
|
@ -69,10 +69,6 @@ namespace CalculationManager
|
|||||||
|
|
||||||
CommandNULL = 0,
|
CommandNULL = 0,
|
||||||
|
|
||||||
// No new command should not be added before CommandSign, 80
|
|
||||||
// If it is needed, the following two functions need to be revised too.
|
|
||||||
// CalculatorManager::MapCommandForSerialize(Command command);
|
|
||||||
// CalculatorManager::MapCommandForDeSerialize(unsigned char command);
|
|
||||||
CommandSIGN = 80,
|
CommandSIGN = 80,
|
||||||
CommandCLEAR = 81,
|
CommandCLEAR = 81,
|
||||||
CommandCENTR = 82,
|
CommandCENTR = 82,
|
||||||
|
@ -1655,66 +1655,6 @@ void StandardCalculatorViewModel::UpdateOperand(int pos, String ^ text)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StandardCalculatorViewModel::UpdateCommandsInRecordingMode()
|
|
||||||
{
|
|
||||||
shared_ptr<vector<int>> commands = make_shared<vector<int>>();
|
|
||||||
bool isDecimal = false;
|
|
||||||
bool isNegative = false;
|
|
||||||
bool isExpMode = false;
|
|
||||||
bool ePlusMode = false;
|
|
||||||
bool eMinusMode = false;
|
|
||||||
|
|
||||||
for (const auto savedCommand : m_standardCalculatorManager.GetSavedCommands())
|
|
||||||
{
|
|
||||||
const Command val = static_cast<Command>(savedCommand);
|
|
||||||
if (val == Command::CommandSIGN)
|
|
||||||
{
|
|
||||||
isNegative = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if ((val >= Command::Command0 && val <= Command::Command9))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else if (val == Command::CommandPNT)
|
|
||||||
{
|
|
||||||
isDecimal = true;
|
|
||||||
}
|
|
||||||
else if (val == Command::CommandEXP)
|
|
||||||
{
|
|
||||||
isExpMode = true;
|
|
||||||
}
|
|
||||||
else if (isExpMode && !ePlusMode && (val == Command::CommandMPLUS))
|
|
||||||
{
|
|
||||||
ePlusMode = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (isExpMode && !eMinusMode && (val == Command::CommandMMINUS))
|
|
||||||
{
|
|
||||||
eMinusMode = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Reset all vars
|
|
||||||
isDecimal = false;
|
|
||||||
isNegative = false;
|
|
||||||
isExpMode = false;
|
|
||||||
ePlusMode = false;
|
|
||||||
eMinusMode = false;
|
|
||||||
commands->clear();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
commands->push_back(static_cast<int>(val));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!commands->empty())
|
|
||||||
{
|
|
||||||
shared_ptr<IOpndCommand> sp = make_shared<COpndCommand>(commands, isNegative, isDecimal, isExpMode);
|
|
||||||
m_commands->push_back(sp);
|
|
||||||
}
|
|
||||||
Recalculate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StandardCalculatorViewModel::OnMaxDigitsReached()
|
void StandardCalculatorViewModel::OnMaxDigitsReached()
|
||||||
{
|
{
|
||||||
if (m_localizedMaxDigitsReachedAutomationFormat == nullptr)
|
if (m_localizedMaxDigitsReachedAutomationFormat == nullptr)
|
||||||
|
@ -38,7 +38,6 @@ namespace CalculatorApp
|
|||||||
public:
|
public:
|
||||||
StandardCalculatorViewModel();
|
StandardCalculatorViewModel();
|
||||||
void UpdateOperand(int pos, Platform::String ^ text);
|
void UpdateOperand(int pos, Platform::String ^ text);
|
||||||
void UpdateCommandsInRecordingMode();
|
|
||||||
|
|
||||||
OBSERVABLE_OBJECT_CALLBACK(OnPropertyChanged);
|
OBSERVABLE_OBJECT_CALLBACK(OnPropertyChanged);
|
||||||
OBSERVABLE_PROPERTY_RW(Platform::String ^, DisplayValue);
|
OBSERVABLE_PROPERTY_RW(Platform::String ^, DisplayValue);
|
||||||
|
Loading…
Reference in New Issue
Block a user