forked from microsoft/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorManager.h
146 lines (124 loc) · 5.61 KB
/
CalculatorManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "CalculatorHistory.h"
#include "Header Files/CalcEngine.h"
#include "Header Files/Rational.h"
#include "Header Files/ICalcDisplay.h"
namespace CalculationManager
{
enum class Command;
struct HISTORYITEM;
enum class CalculatorMode
{
StandardMode,
ScientificMode,
ProgrammerMode,
};
enum class CalculatorPrecision
{
StandardModePrecision = 16,
ScientificModePrecision = 32,
ProgrammerModePrecision = 64
};
// Numbering continues from the Enum Command from Command.h
// with some gap to ensure there is no overlap of these ids
// when static_cast<unsigned char> is performed on these ids
// they shouldn't fall in any number range greater than 80. So never
// make the memory command ids go below 330
enum class MemoryCommand
{
MemorizeNumber = 330,
MemorizedNumberLoad = 331,
MemorizedNumberAdd = 332,
MemorizedNumberSubtract = 333,
MemorizedNumberClearAll = 334,
MemorizedNumberClear = 335
};
class CalculatorManager final : public ICalcDisplay
{
private:
ICalcDisplay* const m_displayCallback;
CCalcEngine* m_currentCalculatorEngine;
std::unique_ptr<CCalcEngine> m_scientificCalculatorEngine;
std::unique_ptr<CCalcEngine> m_standardCalculatorEngine;
std::unique_ptr<CCalcEngine> m_programmerCalculatorEngine;
IResourceProvider* const m_resourceProvider;
bool m_inHistoryItemLoadMode;
std::vector<CalcEngine::Rational> m_memorizedNumbers;
CalcEngine::Rational m_persistedPrimaryValue;
bool m_isExponentialFormat;
static const unsigned int m_maximumMemorySize = 100;
// For persistence
std::vector<unsigned char> m_savedCommands;
std::vector<long> m_savedPrimaryValue;
std::vector<long> m_currentSerializedMemory;
Command m_currentDegreeMode;
Command m_savedDegreeMode;
unsigned char MapCommandForSerialize(Command command);
unsigned int MapCommandForDeSerialize(unsigned char command);
void SaveMemoryCommand(_In_ MemoryCommand command, _In_ unsigned int indexOfMemory);
void MemorizedNumberSelect(_In_ unsigned int);
void MemorizedNumberChanged(_In_ unsigned int);
void LoadPersistedPrimaryValue();
static std::vector<long> SerializeRational(CalcEngine::Rational const& rat);
static CalcEngine::Rational DeSerializeRational(std::vector<long>::const_iterator itr);
static std::vector<long> SerializeNumber(CalcEngine::Number const& num);
static CalcEngine::Number DeSerializeNumber(std::vector<long>::const_iterator itr);
std::shared_ptr<CalculatorHistory> m_pStdHistory;
std::shared_ptr<CalculatorHistory> m_pSciHistory;
CalculatorHistory* m_pHistory;
public:
// ICalcDisplay
void SetPrimaryDisplay(_In_ const std::wstring& displayString, _In_ bool isError) override;
void SetIsInError(bool isError) override;
void SetExpressionDisplay(
_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;
void OnNoRightParenAdded() override;
void DisplayPasteError();
void MaxDigitsReached() override;
void BinaryOperatorReceived() override;
void MemoryItemChanged(unsigned int indexOfMemory) override;
void InputChanged() override;
CalculatorManager(ICalcDisplay* displayCallback, IResourceProvider* resourceProvider);
void Reset(bool clearMemory = true);
void SetStandardMode();
void SetScientificMode();
void SetProgrammerMode();
void SendCommand(_In_ Command command);
void MemorizeNumber();
void MemorizedNumberLoad(_In_ unsigned int);
void MemorizedNumberAdd(_In_ unsigned int);
void MemorizedNumberSubtract(_In_ unsigned int);
void MemorizedNumberClear(_In_ unsigned int);
void MemorizedNumberClearAll();
bool IsEngineRecording();
bool IsInputEmpty();
std::vector<unsigned char> GetSavedCommands()
{
return m_savedCommands;
}
void SetRadix(RADIX_TYPE iRadixType);
void SetMemorizedNumbersString();
std::wstring GetResultForRadix(uint32_t radix, int32_t precision, bool groupDigitsPerRadix);
void SetPrecision(int32_t precision);
void UpdateMaxIntDigits();
wchar_t DecimalSeparator();
std::vector<std::shared_ptr<HISTORYITEM>> const& GetHistoryItems();
std::vector<std::shared_ptr<HISTORYITEM>> const& GetHistoryItems(_In_ CalculationManager::CALCULATOR_MODE mode);
std::shared_ptr<HISTORYITEM> const& GetHistoryItem(_In_ unsigned int uIdx);
bool RemoveHistoryItem(_In_ unsigned int uIdx);
void ClearHistory();
size_t MaxHistorySize() const
{
return m_pHistory->MaxHistorySize();
}
CalculationManager::Command GetCurrentDegreeMode();
void SetHistory(_In_ CALCULATOR_MODE eMode, _In_ std::vector<std::shared_ptr<HISTORYITEM>> const& history);
void SetInHistoryItemLoadMode(_In_ bool isHistoryItemLoadMode);
};
}