calculator/src/CalcViewModel/Common/ExpressionCommandSerializer.cpp
Scott Freeman 6366e0c535 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.
2019-10-30 10:55:13 -07:00

52 lines
1.5 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "Common/ExpressionCommandSerializer.h"
using namespace CalculatorApp::Common;
using namespace Windows::Storage::Streams;
SerializeCommandVisitor::SerializeCommandVisitor(_In_ DataWriter ^ dataWriter)
: m_dataWriter(dataWriter)
{
}
void SerializeCommandVisitor::Visit(_In_ COpndCommand& opndCmd)
{
m_dataWriter->WriteBoolean(opndCmd.IsNegative());
m_dataWriter->WriteBoolean(opndCmd.IsDecimalPresent());
m_dataWriter->WriteBoolean(opndCmd.IsSciFmt());
const auto& opndCmds = opndCmd.GetCommands();
unsigned int opndCmdSize = static_cast<unsigned int>(opndCmds->size());
m_dataWriter->WriteUInt32(opndCmdSize);
for (int eachOpndcmd : *opndCmds)
{
m_dataWriter->WriteInt32(eachOpndcmd);
}
}
void SerializeCommandVisitor::Visit(_In_ CUnaryCommand& unaryCmd)
{
const auto& cmds = unaryCmd.GetCommands();
unsigned int cmdSize = static_cast<unsigned int>(cmds->size());
m_dataWriter->WriteUInt32(cmdSize);
for (int eachOpndcmd : *cmds)
{
m_dataWriter->WriteInt32(eachOpndcmd);
}
}
void SerializeCommandVisitor::Visit(_In_ CBinaryCommand& binaryCmd)
{
int cmd = binaryCmd.GetCommand();
m_dataWriter->WriteInt32(cmd);
}
void SerializeCommandVisitor::Visit(_In_ CParentheses& paraCmd)
{
int parenthesisCmd = paraCmd.GetCommand();
m_dataWriter->WriteInt32(parenthesisCmd);
}