Remove Serialize/Deserialize functions never used in StandardCalculatorViewModel, UnitConverter, UnitConverterViewModel and CalculatorManager (#392)

* remove unused serializer

* remove all unused serialization/deserialization from StandardCalculatorViewModel and UnitConverterViewModel

* formatting
This commit is contained in:
Rudy Huyn
2019-05-09 11:01:43 -07:00
committed by Pepe Rivera
parent 750130c2bc
commit 28888d8df1
13 changed files with 12 additions and 771 deletions

View File

@@ -244,8 +244,6 @@ namespace CalculationManager
{
m_savedCommands.push_back(MapCommandForSerialize(command));
}
this->SerializePrimaryDisplay();
this->SerializeMemory();
m_savedDegreeMode = m_currentDegreeMode;
return;
}
@@ -330,47 +328,6 @@ namespace CalculationManager
return commandToLoad;
}
/// <summary>
/// Return saved degree mode which is saved when last time the expression was cleared.
/// </summary>
Command CalculatorManager::SerializeSavedDegreeMode()
{
return m_savedDegreeMode;
}
void CalculatorManager::SerializePrimaryDisplay()
{
m_savedPrimaryValue.clear();
m_currentCalculatorEngine->ProcessCommand(IDC_STORE);
auto memoryObject = m_currentCalculatorEngine->PersistedMemObject();
if (memoryObject != nullptr)
{
m_savedPrimaryValue = SerializeRational(*memoryObject);
}
}
/// <summary>
/// Return serialized primary display that is saved when the expression line was cleared.
/// </summary>
vector<long> CalculatorManager::GetSerializedPrimaryDisplay()
{
return m_savedPrimaryValue;
}
/// <summary>
/// DeSerialize the primary display from vector of long
/// </summary>
/// <param name = "serializedPrimaryDisplay">Serialized Rational of primary display</param>
void CalculatorManager::DeSerializePrimaryDisplay(const vector<long>& serializedPrimaryDisplay)
{
if (serializedPrimaryDisplay.empty())
{
return;
}
m_persistedPrimaryValue = DeSerializeRational(serializedPrimaryDisplay.begin());
this->LoadPersistedPrimaryValue();
}
/// <summary>
/// Load the persisted value that is saved in memory of CalcEngine
/// </summary>
@@ -380,112 +337,6 @@ namespace CalculationManager
m_currentCalculatorEngine->ProcessCommand(IDC_RECALL);
}
/// <summary>
/// Serialize the Memory to vector of long
/// </summary>
/// <return type = "std::vector<long>">Serialized Rational of memory</return>
void CalculatorManager::SerializeMemory()
{
m_serializedMemory.clear();
for (auto const& memoryItem : m_memorizedNumbers)
{
auto serialMem = SerializeRational(memoryItem);
m_serializedMemory.insert(m_serializedMemory.end(), serialMem.begin(), serialMem.end());
}
}
vector<long> CalculatorManager::GetSerializedMemory()
{
return m_serializedMemory;
}
/// <summary>
/// DeSerialize the Memory from vector of long
/// </summary>
/// <param name = "serializedMemory">Serialized Rational of memory</param>
void CalculatorManager::DeSerializeMemory(const vector<long>& serializedMemory)
{
vector<long>::const_iterator itr = serializedMemory.begin();
while (itr != serializedMemory.end())
{
Rational memoryItem = DeSerializeRational(itr);
auto lengthMemoryItem = (2 * SERIALIZED_NUMBER_MINSIZE) + memoryItem.P().Mantissa().size() + memoryItem.Q().Mantissa().size();
m_memorizedNumbers.push_back(memoryItem);
itr += lengthMemoryItem;
}
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

@@ -63,7 +63,6 @@ namespace CalculationManager
// For persistence
std::vector<unsigned char> m_savedCommands;
std::vector<long> m_savedPrimaryValue;
std::vector<long> m_serializedMemory;
std::vector<long> m_currentSerializedMemory;
Command m_currentDegreeMode;
Command m_savedDegreeMode;
@@ -111,15 +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);
void SerializePrimaryDisplay();
std::vector<long> GetSerializedPrimaryDisplay();
void DeSerializePrimaryDisplay(const std::vector<long>& serializedPrimaryDisplay);
Command SerializeSavedDegreeMode();
void MemorizeNumber();
void MemorizedNumberLoad(_In_ unsigned int);

View File

@@ -11,8 +11,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;
@@ -217,18 +215,6 @@ vector<wstring> UnitConverter::StringToVector(const wstring& w, const wchar_t* d
}
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);
@@ -253,150 +239,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)
{
ClearValues();
ResetCategoriesAndRatios();
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

@@ -237,8 +237,6 @@ namespace UnitConversionManager
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;
@@ -262,8 +260,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;
@@ -290,8 +286,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();