diff --git a/src/CalcManager/CalculatorManager.cpp b/src/CalcManager/CalculatorManager.cpp index ddb28a6b..34888342 100644 --- a/src/CalcManager/CalculatorManager.cpp +++ b/src/CalcManager/CalculatorManager.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" @@ -417,75 +417,6 @@ namespace CalculationManager this->SetMemorizedNumbersString(); } - /// - /// Return the commands saved since the expression has been cleared. - /// - vector CalculatorManager::SerializeCommands() - { - return m_savedCommands; - } - - /// - /// Replay the serialized commands - /// - /// Serialized commands - void CalculatorManager::DeSerializeCommands(_In_ const vector& 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(*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(MapCommandForDeSerialize(*commandItr))); - } - } - } - /// /// Memorize the current displayed value /// Notify the client with new the new memorize value vector diff --git a/src/CalcManager/CalculatorManager.h b/src/CalcManager/CalculatorManager.h index 0b9986b0..7b6236d4 100644 --- a/src/CalcManager/CalculatorManager.h +++ b/src/CalcManager/CalculatorManager.h @@ -110,8 +110,6 @@ namespace CalculationManager void SetScientificMode(); void SetProgrammerMode(); void SendCommand(_In_ Command command); - std::vector SerializeCommands(); - void DeSerializeCommands(_In_ const std::vector& serializedData); void SerializeMemory(); std::vector GetSerializedMemory(); void DeSerializeMemory(const std::vector &serializedMemory); diff --git a/src/CalcManager/UnitConverter.cpp b/src/CalcManager/UnitConverter.cpp index 0548719a..957f7faf 100644 --- a/src/CalcManager/UnitConverter.cpp +++ b/src/CalcManager/UnitConverter.cpp @@ -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 UnitConverter::StringToVector(const wstring& w, const wchar_t * } return serializedTokens; } - -Category UnitConverter::StringToCategory(const wstring& w) -{ - vector 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 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(); -} - -/// -/// Serializes the data in the converter and returns it as a string -/// -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; -} - -/// -/// De-Serializes the data in the converter from a string -/// -/// wstring holding the serialized data. If it does not have expected number of parameters, we will ignore it -void UnitConverter::DeSerialize(const wstring& serializedData) -{ - Reset(); - - if (serializedData.empty()) - { - return; - } - - vector outerTokens = StringToVector(serializedData, L"|"); - assert(outerTokens.size() == EXPECTEDSERIALIZEDTOKENCOUNT); - m_fromType = StringToUnit(outerTokens[0]); - m_toType = StringToUnit(outerTokens[1]); - m_currentCategory = StringToCategory(outerTokens[2]); - vector 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 categoryListTokens = StringToVector(outerTokens[4], L","); - for (wstring token : categoryListTokens) - { - m_categories.push_back(StringToCategory(token)); - } - vector unitVectorTokens = StringToVector(outerTokens[5], L"]"); - for (wstring unitVector : unitVectorTokens) - { - vector mapcomponents = StringToVector(unitVector, L"["); - assert(mapcomponents.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT); - Category key = StringToCategory(mapcomponents[0]); - vector units = StringToVector(mapcomponents[1], L","); - for (wstring unit : units) - { - m_categoryToUnits[key].push_back(StringToUnit(unit)); - } - } - vector ratioMapTokens = StringToVector(outerTokens[6], L"]"); - for (wstring token : ratioMapTokens) - { - vector ratioMapComponentTokens = StringToVector(token, L"["); - assert(ratioMapComponentTokens.size() == EXPECTEDMAPCOMPONENTTOKENCOUNT); - Unit key = StringToUnit(ratioMapComponentTokens[0]); - vector ratioMapList = StringToVector(ratioMapComponentTokens[1], L","); - for (wstring subtoken : ratioMapList) - { - vector 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; } /// diff --git a/src/CalcManager/UnitConverter.h b/src/CalcManager/UnitConverter.h index 4e130d12..ee18ea4a 100644 --- a/src/CalcManager/UnitConverter.h +++ b/src/CalcManager/UnitConverter.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 @@ -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 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(); diff --git a/src/CalcViewModel/StandardCalculatorViewModel.cpp b/src/CalcViewModel/StandardCalculatorViewModel.cpp index c01c4789..f75a2ba0 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.cpp +++ b/src/CalcViewModel/StandardCalculatorViewModel.cpp @@ -1162,110 +1162,6 @@ void StandardCalculatorViewModel::OnMemoryClear(_In_ Object^ memoryItemPosition) } } -Array^ StandardCalculatorViewModel::Serialize() -{ - DataWriter^ writer = ref new DataWriter(); - writer->WriteUInt32(static_cast(m_CurrentAngleType)); - writer->WriteBoolean(IsFToEChecked); - writer->WriteBoolean(IsCurrentViewPinned); - writer->WriteUInt32(static_cast(m_standardCalculatorManager.SerializeSavedDegreeMode())); - - // Serialize Memory - vector serializedMemory; - serializedMemory = m_standardCalculatorManager.GetSerializedMemory(); - size_t lengthOfSerializedMemory = serializedMemory.size(); - writer->WriteUInt32(static_cast(lengthOfSerializedMemory)); - for (auto data : serializedMemory) - { - writer->WriteInt32(data); - } - - // Serialize Primary Display - vector serializedPrimaryDisplay = m_standardCalculatorManager.GetSerializedPrimaryDisplay(); - writer->WriteUInt32(static_cast(serializedPrimaryDisplay.size())); - for (auto data : serializedPrimaryDisplay) - { - writer->WriteInt32(data); - } - - // For ProgrammerMode - writer->WriteUInt32(static_cast(CurrentRadixType)); - - // Serialize commands of calculator manager - vector serializedCommand = m_standardCalculatorManager.SerializeCommands(); - writer->WriteUInt32(static_cast(serializedCommand.size())); - writer->WriteBytes(ref new Array(serializedCommand.data(), static_cast(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^ viewModelDataAsBytes = ref new Array(buffer->Length); - reader->ReadBytes(viewModelDataAsBytes); - - // Return byte array - return viewModelDataAsBytes; -} - -void StandardCalculatorViewModel::Deserialize(Array^ 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(reader->ReadUInt32()); - - m_standardCalculatorManager.SendCommand(serializedDegreeMode); - - // Deserialize Memory - UINT32 memoryDataLength = reader->ReadUInt32(); - vector 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 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^ modelDataAsBytes = ref new Array(modeldatalength); - reader->ReadBytes(modelDataAsBytes); - m_standardCalculatorManager.DeSerializeCommands(vector(modelDataAsBytes->begin(), modelDataAsBytes->end())); - - // After recalculation. If there is an error then - // IsInError should be set synchronously. - if (IsInError) - { - shared_ptr>> commandVector = Utils::DeserializeCommands(reader); - shared_ptr>> tokenVector = Utils::DeserializeTokens(reader); - SetExpressionDisplay(tokenVector, commandVector); - } - } -} - void StandardCalculatorViewModel::OnPropertyChanged(String^ propertyname) { if (propertyname == IsScientificPropertyName) diff --git a/src/CalcViewModel/StandardCalculatorViewModel.h b/src/CalcViewModel/StandardCalculatorViewModel.h index ba6dc82b..a809d0f3 100644 --- a/src/CalcViewModel/StandardCalculatorViewModel.h +++ b/src/CalcViewModel/StandardCalculatorViewModel.h @@ -291,9 +291,6 @@ namespace CalculatorApp void OnBinaryOperatorReceived(); void OnMemoryItemChanged(unsigned int indexOfMemory); - Platform::Array^ Serialize(); - void Deserialize(Platform::Array^ state); - Platform::String^ GetLocalizedStringFormat(Platform::String^ format, Platform::String^ displayValue); void OnPropertyChanged(Platform::String^ propertyname); void SetCalculatorType(CalculatorApp::Common::ViewMode targetState); diff --git a/src/CalcViewModel/UnitConverterViewModel.cpp b/src/CalcViewModel/UnitConverterViewModel.cpp index a04b584e..60103c06 100644 --- a/src/CalcViewModel/UnitConverterViewModel.cpp +++ b/src/CalcViewModel/UnitConverterViewModel.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" @@ -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(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 tokens = UCM::UnitConverter::StringToVector(serializedData, L"[###]"); - assert(tokens.size() >= 2); - vector 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() { diff --git a/src/CalcViewModel/UnitConverterViewModel.h b/src/CalcViewModel/UnitConverterViewModel.h index 5a982384..c70f27a7 100644 --- a/src/CalcViewModel/UnitConverterViewModel.h +++ b/src/CalcViewModel/UnitConverterViewModel.h @@ -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();