remove unused serializer

This commit is contained in:
Rudy Huyn 2019-03-24 15:44:41 -07:00
commit d02ca3918f
8 changed files with 11 additions and 388 deletions

View file

@ -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"
@ -417,75 +417,6 @@ namespace CalculationManager
this->SetMemorizedNumbersString();
}
/// <summary>
/// Return the commands saved since the expression has been cleared.
/// </summary>
vector<unsigned char> CalculatorManager::SerializeCommands()
{
return m_savedCommands;
}
/// <summary>
/// Replay the serialized commands
/// </summary>
/// <param name = "serializedData">Serialized commands</param>
void CalculatorManager::DeSerializeCommands(_In_ const vector<unsigned char>& serializedData)
{
m_savedCommands.clear();
for (auto commandItr = serializedData.begin(); commandItr != serializedData.end(); ++commandItr)
{
if (*commandItr >= MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizeNumber) &&
*commandItr <= MEMORY_COMMAND_TO_UNSIGNED_CHAR(MemoryCommand::MemorizedNumberClearAll))
{
// MemoryCommands(which have values above 255) are pushed on m_savedCommands upon casting to unsigned char.
// SerializeCommands uses m_savedCommands, which is then used in DeSerializeCommands.
// Hence, a simple cast to MemoryCommand is not sufficient.
MemoryCommand memoryCommand = static_cast<MemoryCommand>(*commandItr + UCHAR_MAX + 1);
unsigned int indexOfMemory = 0;
switch (memoryCommand)
{
case MemoryCommand::MemorizeNumber:
this->MemorizeNumber();
break;
case MemoryCommand::MemorizedNumberLoad:
if (commandItr + 1 == serializedData.end())
{
throw out_of_range("Expecting index of memory, data ended prematurely");
}
indexOfMemory = *(++commandItr);
this->MemorizedNumberLoad(indexOfMemory);
break;
case MemoryCommand::MemorizedNumberAdd:
if (commandItr + 1 == serializedData.end())
{
throw out_of_range("Expecting index of memory, data ended prematurely");
}
indexOfMemory = *(++commandItr);
this->MemorizedNumberAdd(indexOfMemory);
break;
case MemoryCommand::MemorizedNumberSubtract:
if (commandItr + 1 == serializedData.end())
{
throw out_of_range("Expecting index of memory, data ended prematurely");
}
indexOfMemory = *(++commandItr);
this->MemorizedNumberSubtract(indexOfMemory);
break;
case MemoryCommand::MemorizedNumberClearAll:
this->MemorizedNumberClearAll();
break;
default:
break;
}
}
else
{
this->SendCommand(static_cast<Command>(MapCommandForDeSerialize(*commandItr)));
}
}
}
/// <summary>
/// Memorize the current displayed value
/// Notify the client with new the new memorize value vector

View file

@ -110,8 +110,6 @@ namespace CalculationManager
void SetScientificMode();
void SetProgrammerMode();
void SendCommand(_In_ Command command);
std::vector<unsigned char> SerializeCommands();
void DeSerializeCommands(_In_ const std::vector<unsigned char>& serializedData);
void SerializeMemory();
std::vector<long> GetSerializedMemory();
void DeSerializeMemory(const std::vector<long> &serializedMemory);

View file

@ -9,8 +9,6 @@ using namespace concurrency;
using namespace std;
using namespace UnitConversionManager;
static constexpr uint32_t EXPECTEDSERIALIZEDTOKENCOUNT = 7;
static constexpr uint32_t EXPECTEDSERIALIZEDCONVERSIONDATATOKENCOUNT = 3;
static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3;
static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6;
static constexpr uint32_t EXPECTEDSTATEDATATOKENCOUNT = 5;
@ -215,18 +213,6 @@ vector<wstring> UnitConverter::StringToVector(const wstring& w, const wchar_t *
}
return serializedTokens;
}
Category UnitConverter::StringToCategory(const wstring& w)
{
vector<wstring> tokenList = StringToVector(w, L";");
assert(tokenList.size() == EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT);
Category serializedCategory;
serializedCategory.id = _wtoi(Unquote(tokenList[0]).c_str());
serializedCategory.supportsNegative = (tokenList[1].compare(L"1") == 0);
serializedCategory.name = Unquote(tokenList[2]);
return serializedCategory;
}
wstring UnitConverter::UnitToString(const Unit& u, const wchar_t * delimiter)
{
wstringstream out(wstringstream::out);
@ -249,146 +235,15 @@ Unit UnitConverter::StringToUnit(const wstring& w)
return serializedUnit;
}
ConversionData UnitConverter::StringToConversionData(const wstring& w)
Category UnitConverter::StringToCategory(const wstring& w)
{
vector<wstring> tokenList = StringToVector(w, L";");
assert(tokenList.size() == EXPECTEDSERIALIZEDCONVERSIONDATATOKENCOUNT);
ConversionData serializedConversionData;
serializedConversionData.ratio = stod(Unquote(tokenList[0]).c_str());
serializedConversionData.offset = stod(Unquote(tokenList[1]).c_str());
serializedConversionData.offsetFirst = (tokenList[2].compare(L"1") == 0);
return serializedConversionData;
}
wstring UnitConverter::ConversionDataToString(ConversionData d, const wchar_t * delimiter)
{
wstringstream out(wstringstream::out);
out.precision(32);
out << fixed << d.ratio;
wstring ratio = out.str();
out.str(L"");
out << fixed << d.offset;
wstring offset = out.str();
out.str(L"");
TrimString(ratio);
TrimString(offset);
out << Quote(ratio) << delimiter << Quote(offset) << delimiter << std::to_wstring(d.offsetFirst) << delimiter;
return out.str();
}
/// <summary>
/// Serializes the data in the converter and returns it as a string
/// </summary>
wstring UnitConverter::Serialize()
{
if (!CheckLoad())
{
return wstring();
}
wstringstream out(wstringstream::out);
const wchar_t * delimiter = L";";
out << UnitToString(m_fromType, delimiter) << "|";
out << UnitToString(m_toType, delimiter) << "|";
out << CategoryToString(m_currentCategory, delimiter) << "|";
out << std::to_wstring(m_currentHasDecimal) << delimiter << std::to_wstring(m_returnHasDecimal) << delimiter << std::to_wstring(m_switchedActive) << delimiter;
out << m_currentDisplay << delimiter << m_returnDisplay << delimiter << "|";
wstringstream categoryString(wstringstream::out);
wstringstream categoryToUnitString(wstringstream::out);
wstringstream unitToUnitToDoubleString(wstringstream::out);
for (const Category& c : m_categories)
{
categoryString << CategoryToString(c, delimiter) << ",";
}
for (const auto& cur : m_categoryToUnits)
{
categoryToUnitString << CategoryToString(cur.first, delimiter) << "[";
for (const Unit& u : cur.second)
{
categoryToUnitString << UnitToString(u, delimiter) << ",";
}
categoryToUnitString << "[" << "]";
}
for (const auto& cur : m_ratioMap)
{
unitToUnitToDoubleString << UnitToString(cur.first, delimiter) << "[";
for (const auto& curConversion : cur.second)
{
unitToUnitToDoubleString << UnitToString(curConversion.first, delimiter) << ":";
unitToUnitToDoubleString << ConversionDataToString(curConversion.second, delimiter) << ":,";
}
unitToUnitToDoubleString << "[" << "]";
}
out << categoryString.str() << "|";
out << categoryToUnitString.str() << "|";
out << unitToUnitToDoubleString.str() << "|";
wstring test = out.str();
return test;
}
/// <summary>
/// De-Serializes the data in the converter from a string
/// </summary>
/// <param name="serializedData">wstring holding the serialized data. If it does not have expected number of parameters, we will ignore it</param>
void UnitConverter::DeSerialize(const wstring& serializedData)
{
Reset();
if (serializedData.empty())
{
return;
}
vector<wstring> outerTokens = StringToVector(serializedData, L"|");
assert(outerTokens.size() == EXPECTEDSERIALIZEDTOKENCOUNT);
m_fromType = StringToUnit(outerTokens[0]);
m_toType = StringToUnit(outerTokens[1]);
m_currentCategory = StringToCategory(outerTokens[2]);
vector<wstring> stateDataTokens = StringToVector(outerTokens[3], L";");
assert(stateDataTokens.size() == EXPECTEDSTATEDATATOKENCOUNT);
m_currentHasDecimal = (stateDataTokens[0].compare(L"1") == 0);
m_returnHasDecimal = (stateDataTokens[1].compare(L"1") == 0);
m_switchedActive = (stateDataTokens[2].compare(L"1") == 0);
m_currentDisplay = stateDataTokens[3];
m_returnDisplay = stateDataTokens[4];
vector<wstring> categoryListTokens = StringToVector(outerTokens[4], L",");
for (wstring token : categoryListTokens)
{
m_categories.push_back(StringToCategory(token));
}
vector<wstring> unitVectorTokens = StringToVector(outerTokens[5], L"]");
for (wstring unitVector : unitVectorTokens)
{
vector<wstring> mapcomponents = StringToVector(unitVector, L"[");
assert(mapcomponents.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
Category key = StringToCategory(mapcomponents[0]);
vector<wstring> units = StringToVector(mapcomponents[1], L",");
for (wstring unit : units)
{
m_categoryToUnits[key].push_back(StringToUnit(unit));
}
}
vector<wstring> ratioMapTokens = StringToVector(outerTokens[6], L"]");
for (wstring token : ratioMapTokens)
{
vector<wstring> ratioMapComponentTokens = StringToVector(token, L"[");
assert(ratioMapComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
Unit key = StringToUnit(ratioMapComponentTokens[0]);
vector<wstring> ratioMapList = StringToVector(ratioMapComponentTokens[1], L",");
for (wstring subtoken : ratioMapList)
{
vector<wstring> ratioMapSubComponentTokens = StringToVector(subtoken, L":");
assert(ratioMapSubComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT);
Unit subkey = StringToUnit(ratioMapSubComponentTokens[0]);
ConversionData conversion = StringToConversionData(ratioMapSubComponentTokens[1]);
m_ratioMap[key][subkey] = conversion;
}
}
UpdateViewModel();
assert(tokenList.size() == EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT);
Category serializedCategory;
serializedCategory.id = _wtoi(Unquote(tokenList[0]).c_str());
serializedCategory.supportsNegative = (tokenList[1].compare(L"1") == 0);
serializedCategory.name = Unquote(tokenList[2]);
return serializedCategory;
}
/// <summary>

View file

@ -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
@ -181,14 +181,12 @@ namespace UnitConversionManager
{
public:
virtual ~IUnitConverter() { }
virtual void Initialize() = 0; // Use to initialize first time, use deserialize instead to rehydrate
virtual void Initialize() = 0;
virtual std::vector<Category> GetCategories() = 0;
virtual CategorySelectionInitializer SetCurrentCategory(const Category& input) = 0;
virtual Category GetCurrentCategory() = 0;
virtual void SetCurrentUnitTypes(const Unit& fromType, const Unit& toType) = 0;
virtual void SwitchActive(const std::wstring& newValue) = 0;
virtual std::wstring Serialize() = 0;
virtual void DeSerialize(const std::wstring& serializedData) = 0;
virtual std::wstring SaveUserPreferences() = 0;
virtual void RestoreUserPreferences(_In_ const std::wstring& userPreferences) = 0;
virtual void SendCommand(Command command) = 0;
@ -210,8 +208,6 @@ namespace UnitConversionManager
Category GetCurrentCategory() override;
void SetCurrentUnitTypes(const Unit& fromType, const Unit& toType) override;
void SwitchActive(const std::wstring& newValue) override;
std::wstring Serialize() override;
void DeSerialize(const std::wstring& serializedData) override;
std::wstring SaveUserPreferences() override;
void RestoreUserPreferences(const std::wstring& userPreference) override;
void SendCommand(Command command) override;
@ -238,8 +234,6 @@ namespace UnitConversionManager
std::wstring CategoryToString(const Category& c, const wchar_t * delimiter);
std::wstring UnitToString(const Unit& u, const wchar_t * delimiter);
Unit StringToUnit(const std::wstring& w);
ConversionData StringToConversionData(const std::wstring& w);
std::wstring ConversionDataToString(ConversionData d, const wchar_t * delimiter);
void UpdateCurrencySymbols();
void UpdateViewModel();
bool AnyUnitIsEmpty();

View file

@ -1162,110 +1162,6 @@ void StandardCalculatorViewModel::OnMemoryClear(_In_ Object^ memoryItemPosition)
}
}
Array<unsigned char>^ StandardCalculatorViewModel::Serialize()
{
DataWriter^ writer = ref new DataWriter();
writer->WriteUInt32(static_cast<UINT32>(m_CurrentAngleType));
writer->WriteBoolean(IsFToEChecked);
writer->WriteBoolean(IsCurrentViewPinned);
writer->WriteUInt32(static_cast<UINT32>(m_standardCalculatorManager.SerializeSavedDegreeMode()));
// Serialize Memory
vector<long> serializedMemory;
serializedMemory = m_standardCalculatorManager.GetSerializedMemory();
size_t lengthOfSerializedMemory = serializedMemory.size();
writer->WriteUInt32(static_cast<UINT32>(lengthOfSerializedMemory));
for (auto data : serializedMemory)
{
writer->WriteInt32(data);
}
// Serialize Primary Display
vector<long> serializedPrimaryDisplay = m_standardCalculatorManager.GetSerializedPrimaryDisplay();
writer->WriteUInt32(static_cast<UINT32>(serializedPrimaryDisplay.size()));
for (auto data : serializedPrimaryDisplay)
{
writer->WriteInt32(data);
}
// For ProgrammerMode
writer->WriteUInt32(static_cast<UINT32>(CurrentRadixType));
// Serialize commands of calculator manager
vector<unsigned char> serializedCommand = m_standardCalculatorManager.SerializeCommands();
writer->WriteUInt32(static_cast<UINT32>(serializedCommand.size()));
writer->WriteBytes(ref new Array<unsigned char>(serializedCommand.data(), static_cast<unsigned int>(serializedCommand.size())));
if (IsInError)
{
Utils::SerializeCommandsAndTokens(m_tokens, m_commands, writer);
}
// Convert viewmodel data in writer to bytes
IBuffer^ buffer = writer->DetachBuffer();
DataReader^ reader = DataReader::FromBuffer(buffer);
Platform::Array<unsigned char>^ viewModelDataAsBytes = ref new Array<unsigned char>(buffer->Length);
reader->ReadBytes(viewModelDataAsBytes);
// Return byte array
return viewModelDataAsBytes;
}
void StandardCalculatorViewModel::Deserialize(Array<unsigned char>^ state)
{
// Read byte array into a buffer
DataWriter^ writer = ref new DataWriter();
writer->WriteBytes(state);
IBuffer^ buffer = writer->DetachBuffer();
// Read view model data
if (buffer->Length != 0)
{
DataReader^ reader = DataReader::FromBuffer(buffer);
m_CurrentAngleType = ConvertIntegerToNumbersAndOperatorsEnum(reader->ReadUInt32());
IsFToEChecked = reader->ReadBoolean();
IsCurrentViewPinned = reader->ReadBoolean();
Command serializedDegreeMode = static_cast<Command>(reader->ReadUInt32());
m_standardCalculatorManager.SendCommand(serializedDegreeMode);
// Deserialize Memory
UINT32 memoryDataLength = reader->ReadUInt32();
vector<long> serializedMemory;
for (unsigned int i = 0; i < memoryDataLength; i++)
{
serializedMemory.push_back(reader->ReadInt32());
}
m_standardCalculatorManager.DeSerializeMemory(serializedMemory);
// Serialize Primary Display
UINT32 serializedPrimaryDisplayLength = reader->ReadUInt32();
vector<long> serializedPrimaryDisplay;
for (unsigned int i = 0; i < serializedPrimaryDisplayLength; i++)
{
serializedPrimaryDisplay.push_back(reader->ReadInt32());
}
m_standardCalculatorManager.DeSerializePrimaryDisplay(serializedPrimaryDisplay);
CurrentRadixType = reader->ReadUInt32();
// Read command data and Deserialize
UINT32 modeldatalength = reader->ReadUInt32();
Array<unsigned char>^ modelDataAsBytes = ref new Array<unsigned char>(modeldatalength);
reader->ReadBytes(modelDataAsBytes);
m_standardCalculatorManager.DeSerializeCommands(vector<unsigned char>(modelDataAsBytes->begin(), modelDataAsBytes->end()));
// After recalculation. If there is an error then
// IsInError should be set synchronously.
if (IsInError)
{
shared_ptr<CalculatorVector<shared_ptr<IExpressionCommand>>> commandVector = Utils::DeserializeCommands(reader);
shared_ptr<CalculatorVector <pair<wstring, int>>> tokenVector = Utils::DeserializeTokens(reader);
SetExpressionDisplay(tokenVector, commandVector);
}
}
}
void StandardCalculatorViewModel::OnPropertyChanged(String^ propertyname)
{
if (propertyname == IsScientificPropertyName)

View file

@ -291,9 +291,6 @@ namespace CalculatorApp
void OnBinaryOperatorReceived();
void OnMemoryItemChanged(unsigned int indexOfMemory);
Platform::Array<unsigned char>^ Serialize();
void Deserialize(Platform::Array<unsigned char>^ state);
Platform::String^ GetLocalizedStringFormat(Platform::String^ format, Platform::String^ displayValue);
void OnPropertyChanged(Platform::String^ propertyname);
void SetCalculatorType(CalculatorApp::Common::ViewMode targetState);

View file

@ -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"
@ -621,52 +621,6 @@ void UnitConverterViewModel::OnPropertyChanged(Platform::String^ prop)
}
}
String^ UnitConverterViewModel::Serialize()
{
wstringstream out(wstringstream::out);
const wchar_t * delimiter = L"[;;;]";
out << std::to_wstring(m_resettingTimer) << delimiter;
out << std::to_wstring(static_cast<int>(m_value1cp)) << delimiter;
out << m_Value1Active << delimiter << m_Value2Active << delimiter;
out << m_Value1->Data() << delimiter << m_Value2->Data() << delimiter;
out << m_valueFromUnlocalized << delimiter << m_valueToUnlocalized << delimiter << L"[###]";
wstring unitConverterSerializedData = m_model->Serialize();
if (!unitConverterSerializedData.empty())
{
out << m_model->Serialize() << L"[###]";
String^ serializedData = ref new String(wstring(out.str()).c_str());
return serializedData;
}
return nullptr;
}
void UnitConverterViewModel::Deserialize(Platform::String^ state)
{
wstring serializedData = wstring(state->Data());
vector<wstring> tokens = UCM::UnitConverter::StringToVector(serializedData, L"[###]");
assert(tokens.size() >= 2);
vector<wstring> viewModelData = UCM::UnitConverter::StringToVector(tokens[0], L"[;;;]");
assert(viewModelData.size() == EXPECTEDVIEWMODELDATATOKENS);
m_resettingTimer = (viewModelData[0].compare(L"1") == 0);
m_value1cp = (ConversionParameter)_wtoi(viewModelData[1].c_str());
m_Value1Active = (viewModelData[2].compare(L"1") == 0);
m_Value2Active = (viewModelData[3].compare(L"1") == 0);
m_Value1 = ref new String(viewModelData[4].c_str());
m_Value2 = ref new String(viewModelData[5].c_str());
m_valueFromUnlocalized = viewModelData[6];
m_valueToUnlocalized = viewModelData[7];
wstringstream modelData(wstringstream::out);
for (unsigned int i = 1; i < tokens.size(); i++)
{
modelData << tokens[i] << L"[###]";
}
m_model->DeSerialize(modelData.str());
InitializeView();
RaisePropertyChanged(nullptr); // Update since all props have been updated.
}
// Saving User Preferences of Category and Associated-Units across Sessions.
void UnitConverterViewModel::SaveUserPreferences()
{

View file

@ -221,8 +221,6 @@ namespace CalculatorApp
Platform::String^ GetLocalizedConversionResultStringFormat(_In_ Platform::String^ fromValue, _In_ Platform::String^ fromUnit, _In_ Platform::String^ toValue, _In_ Platform::String^ toUnit);
void UpdateValue1AutomationName();
void UpdateValue2AutomationName();
Platform::String^ Serialize();
void Deserialize(Platform::String^ state);
// Saving And Restoring User Preferences of Category and Associated-Units across Sessions.
void SaveUserPreferences();