Replacing CalculatorVector usage with std::vector (#756)
* Replacing CalculatorVector usage with std::vector Assumptions made here are that memory allocations are not recoverable. If it can be proved that an index will be in range, then the indexing operation is used. If not (without manual checks) the std::vector::at function is used to throw an exception in case of a programmer bug. * Changes based on PR feedback Using auto& in CalculatorCollector::UpdateHistoryExpression so the token.first value is properly updated. Using range for loop to GenerateExpressions. Setting isEditable directly to the result of boolean expression. Using token.second directly instead of creating a separate tokenCommandIndex variable. * Fixing issue with generating expressions strings. A space should not be added before the first item.
This commit is contained in:
@@ -70,8 +70,8 @@ void CalculatorDisplay::SetIsInError(bool isError)
|
||||
}
|
||||
|
||||
void CalculatorDisplay::SetExpressionDisplay(
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& commands)
|
||||
_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands)
|
||||
{
|
||||
if (m_callbackReference != nullptr)
|
||||
{
|
||||
|
@@ -19,8 +19,8 @@ namespace CalculatorApp
|
||||
void SetPrimaryDisplay(_In_ const std::wstring& displayString, _In_ bool isError) override;
|
||||
void SetIsInError(bool isError) override;
|
||||
void SetExpressionDisplay(
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& commands) override;
|
||||
_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands) override;
|
||||
void SetMemorizedNumbers(_In_ const std::vector<std::wstring>& memorizedNumbers) override;
|
||||
void OnHistoryItemAdded(_In_ unsigned int addedItemIndex) override;
|
||||
void SetParenthesisNumber(_In_ unsigned int parenthesisCount) override;
|
||||
|
@@ -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"
|
||||
@@ -47,13 +47,13 @@ COpndCommand CommandDeserializer::DeserializeOperand()
|
||||
bool fDecimal = m_dataReader->ReadBoolean();
|
||||
bool fSciFmt = m_dataReader->ReadBoolean();
|
||||
|
||||
std::shared_ptr<CalculatorVector<int>> cmdVector = std::make_shared<CalculatorVector<int>>();
|
||||
std::shared_ptr<std::vector<int>> cmdVector = std::make_shared<std::vector<int>>();
|
||||
auto cmdVectorSize = m_dataReader->ReadUInt32();
|
||||
|
||||
for (unsigned int j = 0; j < cmdVectorSize; ++j)
|
||||
{
|
||||
int eachOpndcmd = m_dataReader->ReadInt32();
|
||||
cmdVector->Append(eachOpndcmd);
|
||||
cmdVector->push_back(eachOpndcmd);
|
||||
}
|
||||
|
||||
return COpndCommand(cmdVector, fNegative, fDecimal, fSciFmt);
|
||||
@@ -68,7 +68,6 @@ CParentheses CommandDeserializer::DeserializeParentheses()
|
||||
CUnaryCommand CommandDeserializer::DeserializeUnary()
|
||||
{
|
||||
auto cmdSize = m_dataReader->ReadUInt32();
|
||||
std::shared_ptr<CalculatorVector<int>> cmdVector = std::make_shared<CalculatorVector<int>>();
|
||||
|
||||
if (cmdSize == 1)
|
||||
{
|
||||
|
@@ -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"
|
||||
@@ -18,28 +18,22 @@ void SerializeCommandVisitor::Visit(_In_ COpndCommand& opndCmd)
|
||||
m_dataWriter->WriteBoolean(opndCmd.IsDecimalPresent());
|
||||
m_dataWriter->WriteBoolean(opndCmd.IsSciFmt());
|
||||
|
||||
auto opndCmds = opndCmd.GetCommands();
|
||||
unsigned int opndCmdSize;
|
||||
opndCmds->GetSize(&opndCmdSize);
|
||||
const auto& opndCmds = opndCmd.GetCommands();
|
||||
unsigned int opndCmdSize = static_cast<unsigned int>(opndCmds->size());
|
||||
m_dataWriter->WriteUInt32(opndCmdSize);
|
||||
for (unsigned int j = 0; j < opndCmdSize; ++j)
|
||||
for (int eachOpndcmd : *opndCmds)
|
||||
{
|
||||
int eachOpndcmd;
|
||||
opndCmds->GetAt(j, &eachOpndcmd);
|
||||
m_dataWriter->WriteInt32(eachOpndcmd);
|
||||
}
|
||||
}
|
||||
|
||||
void SerializeCommandVisitor::Visit(_In_ CUnaryCommand& unaryCmd)
|
||||
{
|
||||
auto cmds = unaryCmd.GetCommands();
|
||||
unsigned int cmdSize;
|
||||
cmds->GetSize(&cmdSize);
|
||||
const auto& cmds = unaryCmd.GetCommands();
|
||||
unsigned int cmdSize = static_cast<unsigned int>(cmds->size());
|
||||
m_dataWriter->WriteUInt32(cmdSize);
|
||||
for (unsigned int j = 0; j < cmdSize; ++j)
|
||||
for (int eachOpndcmd : *cmds)
|
||||
{
|
||||
int eachOpndcmd;
|
||||
cmds->GetAt(j, &eachOpndcmd);
|
||||
m_dataWriter->WriteInt32(eachOpndcmd);
|
||||
}
|
||||
}
|
||||
|
@@ -92,36 +92,25 @@ wstring Utils::RemoveUnwantedCharsFromWstring(wstring input, wchar_t* unwantedCh
|
||||
}
|
||||
|
||||
void Utils::SerializeCommandsAndTokens(
|
||||
_In_ shared_ptr<CalculatorVector<pair<wstring, int>>> const& tokens,
|
||||
_In_ shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> const& commands,
|
||||
_In_ shared_ptr<vector<pair<wstring, int>>> const& tokens,
|
||||
_In_ shared_ptr<vector<shared_ptr<IExpressionCommand>>> const& commands,
|
||||
DataWriter ^ writer)
|
||||
{
|
||||
unsigned int commandsSize;
|
||||
IFTPlatformException(commands->GetSize(&commandsSize));
|
||||
|
||||
// Save the size of the commands vector
|
||||
writer->WriteUInt32(commandsSize);
|
||||
writer->WriteUInt32(static_cast<unsigned int>(commands->size()));
|
||||
|
||||
SerializeCommandVisitor cmdVisitor(writer);
|
||||
for (unsigned int i = 0; i < commandsSize; ++i)
|
||||
for (const auto& exprCmd : *commands)
|
||||
{
|
||||
shared_ptr<IExpressionCommand> exprCmd;
|
||||
IFTPlatformException(commands->GetAt(i, &exprCmd));
|
||||
|
||||
CalculationManager::CommandType commandType = exprCmd->GetCommandType();
|
||||
writer->WriteInt32(static_cast<int>(commandType));
|
||||
exprCmd->Accept(cmdVisitor);
|
||||
}
|
||||
|
||||
unsigned int tokensSize;
|
||||
IFTPlatformException(tokens->GetSize(&tokensSize));
|
||||
writer->WriteUInt32(tokensSize);
|
||||
writer->WriteUInt32(static_cast<unsigned int>(tokens->size()));
|
||||
|
||||
for (unsigned int i = 0; i < tokensSize; ++i)
|
||||
for (const auto& eachToken : *tokens)
|
||||
{
|
||||
pair<wstring, int> eachToken;
|
||||
IFTPlatformException(tokens->GetAt(i, &eachToken));
|
||||
|
||||
auto stringData = ref new Platform::String(eachToken.first.c_str());
|
||||
auto intData = eachToken.second;
|
||||
writer->WriteUInt32(writer->MeasureString(stringData));
|
||||
@@ -130,9 +119,9 @@ void Utils::SerializeCommandsAndTokens(
|
||||
}
|
||||
}
|
||||
|
||||
const shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> Utils::DeserializeCommands(DataReader ^ reader)
|
||||
const shared_ptr<vector<shared_ptr<IExpressionCommand>>> Utils::DeserializeCommands(DataReader ^ reader)
|
||||
{
|
||||
shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> commandVector = make_shared<CalculatorVector<shared_ptr<IExpressionCommand>>>();
|
||||
auto commandVector = make_shared<vector<shared_ptr<IExpressionCommand>>>();
|
||||
auto commandVectorSize = reader->ReadUInt32();
|
||||
|
||||
CommandDeserializer cmdDeserializer(reader);
|
||||
@@ -141,26 +130,23 @@ const shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> Utils::Deseri
|
||||
auto commandTypeInt = reader->ReadInt32();
|
||||
CalculationManager::CommandType commandType = static_cast<CalculationManager::CommandType>(commandTypeInt);
|
||||
shared_ptr<IExpressionCommand> exprCmd = cmdDeserializer.Deserialize(commandType);
|
||||
commandVector->Append(exprCmd);
|
||||
commandVector->push_back(exprCmd);
|
||||
}
|
||||
|
||||
return commandVector;
|
||||
}
|
||||
|
||||
const shared_ptr<CalculatorVector<pair<wstring, int>>> Utils::DeserializeTokens(DataReader ^ reader)
|
||||
const shared_ptr<vector<pair<wstring, int>>> Utils::DeserializeTokens(DataReader ^ reader)
|
||||
{
|
||||
shared_ptr<CalculatorVector<pair<wstring, int>>> tokenVector = make_shared<CalculatorVector<pair<wstring, int>>>();
|
||||
auto tokenVector = make_shared<vector<pair<wstring, int>>>();
|
||||
auto tokensSize = reader->ReadUInt32();
|
||||
|
||||
for (unsigned int i = 0; i < tokensSize; ++i)
|
||||
{
|
||||
pair<wstring, int> eachToken;
|
||||
auto stringDataLen = reader->ReadUInt32();
|
||||
auto stringData = reader->ReadString(stringDataLen);
|
||||
auto intData = reader->ReadInt32();
|
||||
eachToken.first = stringData->Data();
|
||||
eachToken.second = intData;
|
||||
tokenVector->Append(eachToken);
|
||||
tokenVector->emplace_back(stringData->Data(), intData);
|
||||
}
|
||||
|
||||
return tokenVector;
|
||||
|
@@ -3,7 +3,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CalcManager/CalculatorVector.h"
|
||||
#include "CalcManager/ExpressionCommandInterface.h"
|
||||
#include "DelegateCommand.h"
|
||||
|
||||
@@ -385,12 +384,12 @@ namespace Utils
|
||||
int GetWindowId();
|
||||
void RunOnUIThreadNonblocking(std::function<void()>&& function, _In_ Windows::UI::Core::CoreDispatcher ^ currentDispatcher);
|
||||
void SerializeCommandsAndTokens(
|
||||
_In_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_In_ std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& commands,
|
||||
_In_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_In_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands,
|
||||
Windows::Storage::Streams::DataWriter ^ writer);
|
||||
|
||||
const std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> DeserializeCommands(Windows::Storage::Streams::DataReader ^ reader);
|
||||
const std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> DeserializeTokens(Windows::Storage::Streams::DataReader ^ reader);
|
||||
const std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> DeserializeCommands(Windows::Storage::Streams::DataReader ^ reader);
|
||||
const std::shared_ptr<std::vector<std::pair<std::wstring, int>>> DeserializeTokens(Windows::Storage::Streams::DataReader ^ reader);
|
||||
|
||||
Windows::Foundation::DateTime GetUniversalSystemTime();
|
||||
bool IsDateTimeOlderThan(Windows::Foundation::DateTime dateTime, const long long duration);
|
||||
|
@@ -13,8 +13,8 @@ using namespace Platform;
|
||||
HistoryItemViewModel::HistoryItemViewModel(
|
||||
String ^ expression,
|
||||
String ^ result,
|
||||
_In_ const shared_ptr<CalculatorVector<pair<wstring, int>>>& spTokens,
|
||||
_In_ const shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>>& spCommands)
|
||||
_In_ const shared_ptr<vector<pair<wstring, int>>>& spTokens,
|
||||
_In_ const shared_ptr<vector<shared_ptr<IExpressionCommand>>>& spCommands)
|
||||
: m_expression(expression)
|
||||
, m_result(result)
|
||||
, m_spTokens(spTokens)
|
||||
@@ -27,37 +27,17 @@ HistoryItemViewModel::HistoryItemViewModel(
|
||||
|
||||
String
|
||||
^ HistoryItemViewModel::GetAccessibleExpressionFromTokens(
|
||||
_In_ shared_ptr<CalculatorVector<pair<wstring, int>>> const& spTokens,
|
||||
_In_ shared_ptr<vector<pair<wstring, int>>> const& spTokens,
|
||||
_In_ String ^ fallbackExpression)
|
||||
{
|
||||
// updating accessibility names for expression and result
|
||||
wstringstream accExpression{};
|
||||
accExpression << L"";
|
||||
|
||||
unsigned int nTokens;
|
||||
HRESULT hr = spTokens->GetSize(&nTokens);
|
||||
if (SUCCEEDED(hr))
|
||||
for (const auto& tokenItem : *spTokens)
|
||||
{
|
||||
pair<wstring, int> tokenItem;
|
||||
for (unsigned int i = 0; i < nTokens; i++)
|
||||
{
|
||||
hr = spTokens->GetAt(i, &tokenItem);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
wstring token = tokenItem.first;
|
||||
accExpression << LocalizationService::GetNarratorReadableToken(StringReference(token.c_str()))->Data();
|
||||
}
|
||||
accExpression << LocalizationService::GetNarratorReadableToken(StringReference(tokenItem.first.c_str()))->Data();
|
||||
}
|
||||
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return LocalizationService::GetNarratorReadableString(fallbackExpression);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ref new String(accExpression.str().c_str());
|
||||
}
|
||||
return ref new String(accExpression.str().c_str());
|
||||
}
|
||||
|
@@ -3,7 +3,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CalcManager/CalculatorVector.h"
|
||||
#include "CalcManager/ExpressionCommandInterface.h"
|
||||
|
||||
namespace CalculatorApp
|
||||
@@ -17,15 +16,15 @@ namespace CalculatorApp
|
||||
HistoryItemViewModel(
|
||||
Platform::String ^ expression,
|
||||
Platform::String ^ result,
|
||||
_In_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& spTokens,
|
||||
_In_ std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& spCommands);
|
||||
_In_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& spTokens,
|
||||
_In_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& spCommands);
|
||||
|
||||
std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& GetTokens()
|
||||
std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& GetTokens()
|
||||
{
|
||||
return m_spTokens;
|
||||
}
|
||||
|
||||
std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& GetCommands()
|
||||
std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& GetCommands()
|
||||
{
|
||||
return m_spCommands;
|
||||
}
|
||||
@@ -62,7 +61,7 @@ namespace CalculatorApp
|
||||
|
||||
private : static Platform::String
|
||||
^ GetAccessibleExpressionFromTokens(
|
||||
_In_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& spTokens,
|
||||
_In_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& spTokens,
|
||||
_In_ Platform::String ^ fallbackExpression);
|
||||
|
||||
private:
|
||||
@@ -70,8 +69,8 @@ namespace CalculatorApp
|
||||
Platform::String ^ m_accExpression;
|
||||
Platform::String ^ m_accResult;
|
||||
Platform::String ^ m_result;
|
||||
std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> m_spTokens;
|
||||
std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> m_spCommands;
|
||||
std::shared_ptr<std::vector<std::pair<std::wstring, int>>> m_spTokens;
|
||||
std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> m_spCommands;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -289,8 +289,8 @@ void StandardCalculatorViewModel::DisableButtons(CommandType selectedExpressionC
|
||||
}
|
||||
|
||||
void StandardCalculatorViewModel::SetExpressionDisplay(
|
||||
_Inout_ shared_ptr<CalculatorVector<pair<wstring, int>>> const& tokens,
|
||||
_Inout_ shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> const& commands)
|
||||
_Inout_ shared_ptr<std::vector<pair<wstring, int>>> const& tokens,
|
||||
_Inout_ shared_ptr<std::vector<shared_ptr<IExpressionCommand>>> const& commands)
|
||||
{
|
||||
m_tokens = tokens;
|
||||
m_commands = commands;
|
||||
@@ -305,11 +305,11 @@ void StandardCalculatorViewModel::SetExpressionDisplay(
|
||||
}
|
||||
|
||||
void StandardCalculatorViewModel::SetHistoryExpressionDisplay(
|
||||
_Inout_ shared_ptr<CalculatorVector<pair<wstring, int>>> const& tokens,
|
||||
_Inout_ shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> const& commands)
|
||||
_Inout_ shared_ptr<vector<pair<wstring, int>>> const& tokens,
|
||||
_Inout_ shared_ptr<vector<shared_ptr<IExpressionCommand>>> const& commands)
|
||||
{
|
||||
m_tokens = make_shared<CalculatorVector<pair<wstring, int>>>(*tokens);
|
||||
m_commands = make_shared<CalculatorVector<shared_ptr<IExpressionCommand>>>(*commands);
|
||||
m_tokens = make_shared<vector<pair<wstring, int>>>(*tokens);
|
||||
m_commands = make_shared<vector<shared_ptr<IExpressionCommand>>>(*commands);
|
||||
IsEditingEnabled = false;
|
||||
|
||||
// Setting the History Item Load Mode so that UI does not get updated with recalculation of every token
|
||||
@@ -319,12 +319,11 @@ void StandardCalculatorViewModel::SetHistoryExpressionDisplay(
|
||||
m_isLastOperationHistoryLoad = true;
|
||||
}
|
||||
|
||||
void StandardCalculatorViewModel::SetTokens(_Inout_ shared_ptr<CalculatorVector<pair<wstring, int>>> const& tokens)
|
||||
void StandardCalculatorViewModel::SetTokens(_Inout_ shared_ptr<vector<pair<wstring, int>>> const& tokens)
|
||||
{
|
||||
AreTokensUpdated = false;
|
||||
|
||||
unsigned int nTokens = 0;
|
||||
tokens->GetSize(&nTokens);
|
||||
const size_t nTokens = tokens->size();
|
||||
|
||||
if (nTokens == 0)
|
||||
{
|
||||
@@ -332,51 +331,48 @@ void StandardCalculatorViewModel::SetTokens(_Inout_ shared_ptr<CalculatorVector<
|
||||
return;
|
||||
}
|
||||
|
||||
pair<wstring, int> currentToken;
|
||||
const auto& localizer = LocalizationSettings::GetInstance();
|
||||
|
||||
const wstring separator = L" ";
|
||||
for (unsigned int i = 0; i < nTokens; ++i)
|
||||
{
|
||||
if (SUCCEEDED(tokens->GetAt(i, ¤tToken)))
|
||||
auto currentToken = (*tokens)[i];
|
||||
|
||||
Common::TokenType type;
|
||||
bool isEditable = currentToken.second != -1;
|
||||
localizer.LocalizeDisplayValue(&(currentToken.first));
|
||||
|
||||
if (!isEditable)
|
||||
{
|
||||
Common::TokenType type;
|
||||
bool isEditable = (currentToken.second == -1) ? false : true;
|
||||
localizer.LocalizeDisplayValue(&(currentToken.first));
|
||||
type = currentToken.first == separator ? TokenType::Separator : TokenType::Operator;
|
||||
}
|
||||
else
|
||||
{
|
||||
const shared_ptr<IExpressionCommand>& command = m_commands->at(currentToken.second);
|
||||
type = command->GetCommandType() == CommandType::OperandCommand ? TokenType::Operand : TokenType::Operator;
|
||||
}
|
||||
|
||||
if (!isEditable)
|
||||
auto currentTokenString = ref new String(currentToken.first.c_str());
|
||||
if (i < m_ExpressionTokens->Size)
|
||||
{
|
||||
auto existingItem = m_ExpressionTokens->GetAt(i);
|
||||
if (type == existingItem->Type && existingItem->Token->Equals(currentTokenString))
|
||||
{
|
||||
type = currentToken.first == separator ? TokenType::Separator : TokenType::Operator;
|
||||
}
|
||||
else
|
||||
{
|
||||
shared_ptr<IExpressionCommand> command;
|
||||
IFTPlatformException(m_commands->GetAt(static_cast<unsigned int>(currentToken.second), &command));
|
||||
type = command->GetCommandType() == CommandType::OperandCommand ? TokenType::Operand : TokenType::Operator;
|
||||
}
|
||||
|
||||
auto currentTokenString = ref new String(currentToken.first.c_str());
|
||||
if (i < m_ExpressionTokens->Size)
|
||||
{
|
||||
auto existingItem = m_ExpressionTokens->GetAt(i);
|
||||
if (type == existingItem->Type && existingItem->Token->Equals(currentTokenString))
|
||||
{
|
||||
existingItem->TokenPosition = i;
|
||||
existingItem->IsTokenEditable = isEditable;
|
||||
existingItem->CommandIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto expressionToken = ref new DisplayExpressionToken(currentTokenString, i, isEditable, type);
|
||||
m_ExpressionTokens->InsertAt(i, expressionToken);
|
||||
}
|
||||
existingItem->TokenPosition = i;
|
||||
existingItem->IsTokenEditable = isEditable;
|
||||
existingItem->CommandIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto expressionToken = ref new DisplayExpressionToken(currentTokenString, i, isEditable, type);
|
||||
m_ExpressionTokens->Append(expressionToken);
|
||||
m_ExpressionTokens->InsertAt(i, expressionToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
auto expressionToken = ref new DisplayExpressionToken(currentTokenString, i, isEditable, type);
|
||||
m_ExpressionTokens->Append(expressionToken);
|
||||
}
|
||||
}
|
||||
|
||||
while (m_ExpressionTokens->Size != nTokens)
|
||||
@@ -1277,16 +1273,12 @@ ANGLE_TYPE GetAngleTypeFromCommand(Command command)
|
||||
|
||||
void StandardCalculatorViewModel::SaveEditedCommand(_In_ unsigned int tokenPosition, _In_ Command command)
|
||||
{
|
||||
pair<wstring, int> token;
|
||||
bool handleOperand = false;
|
||||
int nOpCode = static_cast<int>(command);
|
||||
wstring updatedToken = L"";
|
||||
|
||||
shared_ptr<IExpressionCommand> tokenCommand;
|
||||
IFTPlatformException(m_tokens->GetAt(tokenPosition, &token));
|
||||
|
||||
unsigned int tokenCommandIndex = token.second;
|
||||
IFTPlatformException(m_commands->GetAt(tokenCommandIndex, &tokenCommand));
|
||||
const pair<wstring, int>& token = m_tokens->at(tokenPosition);
|
||||
const shared_ptr<IExpressionCommand>& tokenCommand = m_commands->at(token.second);
|
||||
|
||||
if (IsUnaryOp(nOpCode) && command != Command::CommandSIGN)
|
||||
{
|
||||
@@ -1352,7 +1344,7 @@ void StandardCalculatorViewModel::SaveEditedCommand(_In_ unsigned int tokenPosit
|
||||
if (tokenCommand->GetCommandType() == CommandType::UnaryCommand)
|
||||
{
|
||||
shared_ptr<IExpressionCommand> spSignCommand = make_shared<CUnaryCommand>(nOpCode);
|
||||
IFTPlatformException(m_commands->InsertAt(tokenCommandIndex + 1, spSignCommand));
|
||||
m_commands->insert(m_commands->begin() + token.second + 1, spSignCommand);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1365,12 +1357,8 @@ void StandardCalculatorViewModel::SaveEditedCommand(_In_ unsigned int tokenPosit
|
||||
|
||||
if (!handleOperand)
|
||||
{
|
||||
IFTPlatformException(m_commands->SetAt(tokenCommandIndex, tokenCommand));
|
||||
|
||||
pair<wstring, int> selectedToken;
|
||||
IFTPlatformException(m_tokens->GetAt(tokenPosition, &selectedToken));
|
||||
selectedToken.first = updatedToken;
|
||||
IFTPlatformException(m_tokens->SetAt(tokenPosition, selectedToken));
|
||||
(*m_commands)[token.second] = tokenCommand;
|
||||
(*m_tokens)[tokenPosition].first = updatedToken;
|
||||
|
||||
DisplayExpressionToken ^ displayExpressionToken = ExpressionTokens->GetAt(tokenPosition);
|
||||
displayExpressionToken->Token = ref new Platform::String(updatedToken.c_str());
|
||||
@@ -1388,30 +1376,21 @@ void StandardCalculatorViewModel::Recalculate(bool fromHistory)
|
||||
{
|
||||
// Recalculate
|
||||
Command currentDegreeMode = m_standardCalculatorManager.GetCurrentDegreeMode();
|
||||
shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> savedCommands = make_shared<CalculatorVector<shared_ptr<IExpressionCommand>>>();
|
||||
|
||||
shared_ptr<vector<shared_ptr<IExpressionCommand>>> savedCommands = make_shared<vector<shared_ptr<IExpressionCommand>>>();
|
||||
vector<int> currentCommands;
|
||||
unsigned int commandListCount;
|
||||
m_commands->GetSize(&commandListCount);
|
||||
for (unsigned int i = 0; i < commandListCount; i++)
|
||||
{
|
||||
shared_ptr<IExpressionCommand> command;
|
||||
IFTPlatformException(m_commands->GetAt(i, &command));
|
||||
|
||||
savedCommands->Append(command);
|
||||
for (const auto& command : *m_commands)
|
||||
{
|
||||
savedCommands->push_back(command);
|
||||
CommandType commandType = command->GetCommandType();
|
||||
|
||||
if (commandType == CommandType::UnaryCommand)
|
||||
{
|
||||
shared_ptr<IUnaryCommand> spCommand = dynamic_pointer_cast<IUnaryCommand>(command);
|
||||
shared_ptr<CalculatorVector<int>> unaryCommands = spCommand->GetCommands();
|
||||
unsigned int unaryCommandCount;
|
||||
unaryCommands->GetSize(&unaryCommandCount);
|
||||
const shared_ptr<vector<int>>& unaryCommands = spCommand->GetCommands();
|
||||
|
||||
int nUCode;
|
||||
for (unsigned int j = 0; j < unaryCommandCount; ++j)
|
||||
for (int nUCode : *unaryCommands)
|
||||
{
|
||||
IFTPlatformException(unaryCommands->GetAt(j, &nUCode));
|
||||
currentCommands.push_back(nUCode);
|
||||
}
|
||||
}
|
||||
@@ -1431,15 +1410,11 @@ void StandardCalculatorViewModel::Recalculate(bool fromHistory)
|
||||
if (commandType == CommandType::OperandCommand)
|
||||
{
|
||||
shared_ptr<IOpndCommand> spCommand = dynamic_pointer_cast<IOpndCommand>(command);
|
||||
shared_ptr<CalculatorVector<int>> opndCommands = spCommand->GetCommands();
|
||||
unsigned int opndCommandCount;
|
||||
opndCommands->GetSize(&opndCommandCount);
|
||||
const shared_ptr<vector<int>>& opndCommands = spCommand->GetCommands();
|
||||
bool fNeedIDCSign = spCommand->IsNegative();
|
||||
|
||||
int nOCode;
|
||||
for (unsigned int j = 0; j < opndCommandCount; ++j)
|
||||
for (int nOCode : *opndCommands)
|
||||
{
|
||||
IFTPlatformException(opndCommands->GetAt(j, &nOCode));
|
||||
currentCommands.push_back(nOCode);
|
||||
|
||||
if (fNeedIDCSign && nOCode != IDC_0)
|
||||
@@ -1450,16 +1425,12 @@ void StandardCalculatorViewModel::Recalculate(bool fromHistory)
|
||||
}
|
||||
}
|
||||
}
|
||||
shared_ptr<CalculatorVector<pair<wstring, int>>> savedTokens = make_shared<CalculatorVector<pair<wstring, int>>>();
|
||||
|
||||
unsigned int tokenCount;
|
||||
IFTPlatformException(m_tokens->GetSize(&tokenCount));
|
||||
shared_ptr<vector<pair<wstring, int>>> savedTokens = make_shared<vector<pair<wstring, int>>>();
|
||||
|
||||
for (unsigned int i = 0; i < tokenCount; ++i)
|
||||
for (const auto& currentToken : *m_tokens)
|
||||
{
|
||||
pair<wstring, int> currentToken;
|
||||
IFTPlatformException(m_tokens->GetAt(i, ¤tToken));
|
||||
savedTokens->Append(currentToken);
|
||||
savedTokens->push_back(currentToken);
|
||||
}
|
||||
|
||||
m_standardCalculatorManager.Reset(false);
|
||||
@@ -1497,12 +1468,9 @@ void StandardCalculatorViewModel::Recalculate(bool fromHistory)
|
||||
|
||||
CommandType StandardCalculatorViewModel::GetSelectedTokenType(_In_ unsigned int tokenPosition)
|
||||
{
|
||||
pair<wstring, int> token;
|
||||
shared_ptr<IExpressionCommand> tokenCommand;
|
||||
IFTPlatformException(m_tokens->GetAt(tokenPosition, &token));
|
||||
|
||||
const pair<wstring, int>& token = m_tokens->at(tokenPosition);
|
||||
unsigned int tokenCommandIndex = token.second;
|
||||
IFTPlatformException(m_commands->GetAt(tokenCommandIndex, &tokenCommand));
|
||||
const shared_ptr<IExpressionCommand>& tokenCommand = m_commands->at(tokenCommandIndex);
|
||||
|
||||
return tokenCommand->GetCommandType();
|
||||
}
|
||||
@@ -1715,20 +1683,18 @@ NumbersAndOperatorsEnum StandardCalculatorViewModel::ConvertIntegerToNumbersAndO
|
||||
|
||||
void StandardCalculatorViewModel::UpdateOperand(int pos, String ^ text)
|
||||
{
|
||||
pair<wstring, int> p;
|
||||
m_tokens->GetAt(pos, &p);
|
||||
pair<wstring, int> p = m_tokens->at(pos);
|
||||
|
||||
String ^ englishString = LocalizationSettings::GetInstance().GetEnglishValueFromLocalizedDigits(text->Data());
|
||||
p.first = englishString->Data();
|
||||
|
||||
int commandPos = p.second;
|
||||
shared_ptr<IExpressionCommand> exprCmd;
|
||||
m_commands->GetAt(commandPos, &exprCmd);
|
||||
const shared_ptr<IExpressionCommand>& exprCmd = m_commands->at(commandPos);
|
||||
auto operandCommand = std::dynamic_pointer_cast<IOpndCommand>(exprCmd);
|
||||
|
||||
if (operandCommand != nullptr)
|
||||
{
|
||||
shared_ptr<CalculatorVector<int>> commands = make_shared<CalculatorVector<int>>();
|
||||
shared_ptr<vector<int>> commands = make_shared<vector<int>>();
|
||||
size_t length = p.first.length();
|
||||
if (length > 0)
|
||||
{
|
||||
@@ -1766,12 +1732,12 @@ void StandardCalculatorViewModel::UpdateOperand(int pos, String ^ text)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
commands->Append(num);
|
||||
commands->push_back(num);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
commands->Append(0);
|
||||
commands->push_back(0);
|
||||
}
|
||||
operandCommand->SetCommands(commands);
|
||||
}
|
||||
@@ -1780,7 +1746,7 @@ void StandardCalculatorViewModel::UpdateOperand(int pos, String ^ text)
|
||||
void StandardCalculatorViewModel::UpdatecommandsInRecordingMode()
|
||||
{
|
||||
vector<unsigned char> savedCommands = m_standardCalculatorManager.GetSavedCommands();
|
||||
shared_ptr<CalculatorVector<int>> commands = make_shared<CalculatorVector<int>>();
|
||||
shared_ptr<vector<int>> commands = make_shared<vector<int>>();
|
||||
bool isDecimal = false;
|
||||
bool isNegative = false;
|
||||
bool isExpMode = false;
|
||||
@@ -1827,18 +1793,16 @@ void StandardCalculatorViewModel::UpdatecommandsInRecordingMode()
|
||||
isExpMode = false;
|
||||
ePlusMode = false;
|
||||
eMinusMode = false;
|
||||
commands->Clear();
|
||||
commands->clear();
|
||||
continue;
|
||||
}
|
||||
commands->Append(num);
|
||||
commands->push_back(num);
|
||||
}
|
||||
|
||||
unsigned int size = 0;
|
||||
commands->GetSize(&size);
|
||||
if (size > 0)
|
||||
if (!commands->empty())
|
||||
{
|
||||
shared_ptr<IOpndCommand> sp = make_shared<COpndCommand>(commands, isNegative, isDecimal, isExpMode);
|
||||
m_commands->Append(sp);
|
||||
m_commands->push_back(sp);
|
||||
}
|
||||
Recalculate();
|
||||
}
|
||||
|
@@ -342,13 +342,13 @@ namespace CalculatorApp
|
||||
void OnInputChanged();
|
||||
void SetPrimaryDisplay(_In_ std::wstring const& displayString, _In_ bool isError);
|
||||
void DisplayPasteError();
|
||||
void SetTokens(_Inout_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens);
|
||||
void SetTokens(_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens);
|
||||
void SetExpressionDisplay(
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& commands);
|
||||
_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands);
|
||||
void SetHistoryExpressionDisplay(
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> const& commands);
|
||||
_Inout_ std::shared_ptr<std::vector<std::pair<std::wstring, int>>> const& tokens,
|
||||
_Inout_ std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> const& commands);
|
||||
void SetParenthesisCount(_In_ unsigned int parenthesisCount);
|
||||
void SetOpenParenthesisCountNarratorAnnouncement();
|
||||
void OnNoRightParenAdded();
|
||||
@@ -444,8 +444,8 @@ namespace CalculatorApp
|
||||
std::wstring AddPadding(std::wstring);
|
||||
size_t LengthWithoutPadding(std::wstring);
|
||||
|
||||
std::shared_ptr<CalculatorVector<std::pair<std::wstring, int>>> m_tokens;
|
||||
std::shared_ptr<CalculatorVector<std::shared_ptr<IExpressionCommand>>> m_commands;
|
||||
std::shared_ptr<std::vector<std::pair<std::wstring, int>>> m_tokens;
|
||||
std::shared_ptr<std::vector<std::shared_ptr<IExpressionCommand>>> m_commands;
|
||||
|
||||
// Token types
|
||||
bool IsUnaryOp(int nOpCode);
|
||||
|
Reference in New Issue
Block a user