Simplify some code

This commit is contained in:
Pepe Rivera 2019-04-10 10:33:23 -07:00
commit 09d463d666
4 changed files with 79 additions and 191 deletions

View file

@ -25,13 +25,18 @@ static constexpr wstring_view DEFAULT_NUMBER_STR = L"0";
// Read strings for keys, errors, trig types, etc.
// These will be copied from the resources to local memory.
array<wstring, CSTRINGSENGMAX> CCalcEngine::s_engineStrings;
unordered_map<wstring, wstring> CCalcEngine::s_engineStrings;
void CCalcEngine::LoadEngineStrings(CalculationManager::IResourceProvider& resourceProvider)
{
for (size_t i = 0; i < s_engineStrings.size(); i++)
for (size_t i = 0; i < g_sids.size(); i++)
{
s_engineStrings[i] = resourceProvider.GetCEngineString(g_sids[i]);
auto locKey = g_sids[i];
auto locString = resourceProvider.GetCEngineString(locKey);
if (!locString.empty())
{
s_engineStrings[locKey] = locString;
}
}
}
@ -168,7 +173,7 @@ void CCalcEngine::SettingsChanged()
m_HistoryCollector.SetDecimalSymbol(m_decimalSeparator);
// put the new decimal symbol into the table used to draw the decimal key
s_engineStrings[IDS_DECIMAL] = m_decimalSeparator;
s_engineStrings[SIDS_DECIMAL_SEPARATOR] = m_decimalSeparator;
// we need to redraw to update the decimal point button
numChanged = true;

View file

@ -16,13 +16,6 @@
#include "Header Files/CalcEngine.h"
#include "Header Files/CalcUtils.h"
#define IDC_RADSIN IDC_UNARYLAST+1
#define IDC_RADCOS IDC_UNARYLAST+2
#define IDC_RADTAN IDC_UNARYLAST+3
#define IDC_GRADSIN IDC_UNARYLAST+4
#define IDC_GRADCOS IDC_UNARYLAST+5
#define IDC_GRADTAN IDC_UNARYLAST+6
using namespace std;
using namespace CalcEngine;
@ -870,153 +863,91 @@ void CCalcEngine::DisplayAnnounceBinaryOperator()
// we have this separate table to get its localized name and for its Inv function if it exists.
typedef struct
{
int idsFunc; // index of string for the unary op function. Can be NULL, in which case it same as button name
int idsFuncInv; // index of string for Inv of unary op. Can be NULL, in case it is same as idsFunc
bool fDontUseInExpEval; // true if this cant be used in reverse direction as well, ie. during expression evaluation
wstring degreeString; // index of string for the unary op function. Can be empty string, in which case it same as button name
wstring inverseDegreeString; // index of string for Inv of unary op. Can be empty string, in case it is same as idsFunc
wstring radString; // index of string for the unary op function in rads. Can be empty string, in which case it same as button name
wstring inverseRadString; // index of string for Inv of unary op in rads. Can be empty string, in case it is same as idsFunc
wstring gradString; // index of string for the unary op function in grads. Can be empty string, in which case it same as button name
wstring inverseGradString; // index of string for Inv of unary op in grads. Can be empty string, in case it is same as idsFunc
bool hasAngleStrings = ((!radString.empty()) || (!inverseRadString.empty()) || (!gradString.empty()) || (!inverseGradString.empty()));
} UFNE;
// Table for each unary operator
static const UFNE rgUfne[] =
static const std::unordered_map<int, UFNE> unaryOperatorStringTable =
{
/* IDC_CHOP */{ 0, IDS_FRAC, false },
/* IDC_ROL */{ 0, 0, true },
/* IDC_ROR */{ 0, 0, true },
{ IDC_CHOP, { L"", SIDS_FRAC} },
/* IDC_COM */{ 0, 0, true },
/* IDC_SIN */{ IDS_SIND, IDS_ASIND, false }, // default in this table is degrees for sin,cos & tan
/* IDC_COS */{ IDS_COSD, IDS_ACOSD, false },
/* IDC_TAN */{ IDS_TAND, IDS_ATAND, false },
{ IDC_SIN, { SIDS_SIND, SIDS_ASIND, SIDS_SINR, SIDS_ASINR, SIDS_SING, SIDS_ASING } },
{ IDC_COS, { SIDS_COSD, SIDS_ACOSD, SIDS_COSR, SIDS_ACOSR, SIDS_COSG, SIDS_ACOSG } },
{ IDC_TAN, { SIDS_TAND, SIDS_ATAND, SIDS_TANR, SIDS_ATANR, SIDS_TANG, SIDS_ATANG } },
/* IDC_SINH */{ 0, IDS_ASINH, false },
/* IDC_COSH */{ 0, IDS_ACOSH, false },
/* IDC_TANH */{ 0, IDS_ATANH, false },
{ IDC_SINH, { L"", SIDS_ASINH } },
{ IDC_COSH, { L"", SIDS_ACOSH } },
{ IDC_TANH, { L"", SIDS_ATANH } },
/* IDC_LN */{ 0, IDS_POWE, false },
/* IDC_LOG */{ 0, 0, false },
/* IDC_SQRT */{ 0, 0, false },
/* IDC_SQR */{ IDS_SQR, 0, false },
/* IDC_CUB */{ IDS_CUBE, 0, false },
/* IDC_FAC */{ IDS_FACT, 0, false },
/* IDC_REC */{ IDS_REC, 0, false },
/* IDC_DMS */{ 0, IDS_DEGREES, false },
/* IDC_CUBEROOT */{ 0, 0, false },
/* IDC_POW10 */{ 0, 0, false },
/* IDC_PERCENT */{ 0, 0, false },
/* IDC_RADSIN */{ IDS_SINR, IDS_ASINR, false },
/* IDC_RADCOS */{ IDS_COSR, IDS_ACOSR, false },
/* IDC_RADTAN */{ IDS_TANR, IDS_ATANR, false },
/* IDC_GRADCOS */{ IDS_SING, IDS_ASING, false },
/* IDC_GRADCOS */{ IDS_COSG, IDS_ACOSG, false },
/* IDC_GRADTAN */{ IDS_TANG, IDS_ATANG, false },
{ IDC_LN , { L"", SIDS_POWE } },
{ IDC_SQR, { SIDS_SQR } },
{ IDC_CUB, { SIDS_CUBE } },
{ IDC_FAC, { SIDS_FACT } },
{ IDC_REC, { SIDS_RECIPROCAL } },
{ IDC_DMS, { L"", SIDS_DEGREES } },
{ IDC_SIGN, { SIDS_NEGATE } },
{ IDC_DEGREES, { SIDS_DEGREES } }
};
wstring_view CCalcEngine::OpCodeToUnaryString(int nOpCode, bool fInv, ANGLE_TYPE angletype)
{
// Special cases for Sign and Degrees
if (IDC_SIGN == nOpCode)
// Try to lookup the ID in the UFNE table
wstring ids = L"";
auto pair = unaryOperatorStringTable.find(nOpCode);
if (pair != unaryOperatorStringTable.end())
{
return GetString(IDS_NEGATE);
}
if (IDC_DEGREES == nOpCode)
if (!pair->second.hasAngleStrings || ANGLE_DEG == angletype)
{
return GetString(IDS_DEGREES);
if (fInv)
{
ids = pair->second.inverseDegreeString;
}
// Correct the trigonometric functions with type of angle argument they take
if (ANGLE_RAD == angletype)
if (ids.empty())
{
switch (nOpCode)
ids = pair->second.degreeString;
}
}
else if (ANGLE_RAD == angletype)
{
case IDC_SIN:
nOpCode = IDC_RADSIN;
break;
case IDC_COS:
nOpCode = IDC_RADCOS;
break;
case IDC_TAN:
nOpCode = IDC_RADTAN;
break;
if (fInv)
{
ids = pair->second.inverseRadString;
}
if (ids.empty())
{
ids = pair->second.radString;
}
}
else if (ANGLE_GRAD == angletype)
{
switch (nOpCode)
{
case IDC_SIN:
nOpCode = IDC_GRADSIN;
break;
case IDC_COS:
nOpCode = IDC_GRADCOS;
break;
case IDC_TAN:
nOpCode = IDC_GRADTAN;
break;
}
}
// Try to lookup the ID in the UFNE table
int ids = 0;
int iufne = nOpCode - IDC_UNARYFIRST;
if (iufne >= 0 && (size_t)iufne < size(rgUfne))
{
if (fInv)
{
ids = rgUfne[iufne].idsFuncInv;
ids = pair->second.inverseGradString;
}
if (0 == ids)
if (ids.empty())
{
ids = rgUfne[iufne].idsFunc;
ids = pair->second.gradString;
}
}
}
// If we didn't find an ID in the table, use the op code.
if (0 == ids)
if (!ids.empty())
{
ids = IdStrFromCmdId(nOpCode);
}
return GetString(ids);
}
//
// Sets the Angle Mode for special unary op IDC's which are used to index to the table rgUfne
// and returns the equivalent plain IDC for trigonometric function. If it isn't a trigonometric function
// returns the passed in idc itself.
int CCalcEngine::IdcSetAngleTypeDecMode(int idc)
{
int idcAngleCmd = IDM_DEG;
switch (idc)
{
case IDC_RADSIN:
idcAngleCmd = IDM_RAD;
idc = IDC_SIN;
break;
case IDC_RADCOS:
idcAngleCmd = IDM_RAD;
idc = IDC_COS;
break;
case IDC_RADTAN:
idcAngleCmd = IDM_RAD;
idc = IDC_TAN;
break;
case IDC_GRADSIN:
idcAngleCmd = IDM_GRAD;
idc = IDC_SIN;
break;
case IDC_GRADCOS:
idcAngleCmd = IDM_GRAD;
idc = IDC_COS;
break;
case IDC_GRADTAN:
idcAngleCmd = IDM_GRAD;
idc = IDC_TAN;
break;
}
ProcessCommand(idcAngleCmd);
return idc;
return GetString(IdStrFromCmdId(nOpCode));
}
bool CCalcEngine::IsCurrentTooBigForTrig()

View file

@ -72,7 +72,8 @@ public:
// Static methods for the instance
static void InitialOneTimeOnlySetup(CalculationManager::IResourceProvider& resourceProvider); // Once per load time to call to initialize all shared global variables
// returns the ptr to string representing the operator. Mostly same as the button, but few special cases for x^y etc.
static std::wstring_view GetString(int ids) { return s_engineStrings[ids]; }
static std::wstring_view GetString(int ids) { return s_engineStrings[std::to_wstring(ids)]; }
static std::wstring_view GetString(std::wstring ids) { return s_engineStrings[ids]; }
static std::wstring_view OpCodeToString(int nOpCode) { return GetString(IdStrFromCmdId(nOpCode)); }
static std::wstring_view OpCodeToUnaryString(int nOpCode, bool fInv, ANGLE_TYPE angletype);
@ -123,7 +124,7 @@ private:
std::array<CalcEngine::Rational, NUM_WIDTH_LENGTH> m_chopNumbers; // word size enforcement
std::array<std::wstring, NUM_WIDTH_LENGTH> m_maxDecimalValueStrings; // maximum values represented by a given word width based off m_chopNumbers
static std::array<std::wstring, CSTRINGSENGMAX> s_engineStrings; // the string table shared across all instances
static std::unordered_map<std::wstring, std::wstring> s_engineStrings; // the string table shared across all instances
wchar_t m_decimalSeparator;
wchar_t m_groupSeparator;
@ -146,12 +147,11 @@ private:
bool TryToggleBit(CalcEngine::Rational& rat, uint32_t wbitno);
void CheckAndAddLastBinOpToHistory(bool addToHistory = true);
int IdcSetAngleTypeDecMode(int idc);
void InitChopNumbers();
static void LoadEngineStrings(CalculationManager::IResourceProvider& resourceProvider);
static int IdStrFromCmdId(int id) { return id - IDC_FIRSTCONTROL + IDS_FIRSTENGSTR; }
static int IdStrFromCmdId(int id) { return id - IDC_FIRSTCONTROL + IDS_ENGINESTR_FIRST; }
static std::vector<uint32_t> DigitGroupingStringToGroupingVector(std::wstring_view groupingString);
std::wstring GroupDigits(std::wstring_view delimiter, std::vector<uint32_t> const& grouping, std::wstring_view displayString, bool isNumNegative = false);

View file

@ -13,56 +13,7 @@
* Created: 13-Feb-2008
*
\****************************************************************************/
#define IDS_FIRSTENGSTR IDS_ENGINESTR_FIRST
#define IDS_DECIMAL 4
// All unary op function names for easy history reading
// This is where the first string after all the commands in order have been placed, should be placed
// keeping in consecutive helps us to allocate 1 string table and index them
#define IDS_FNSZFIRST (IDC_F -IDC_FIRSTCONTROL)+1
#define IDS_FRAC IDS_FNSZFIRST
#define IDS_SIND IDS_FNSZFIRST+1
#define IDS_COSD IDS_FNSZFIRST+2
#define IDS_TAND IDS_FNSZFIRST+3
#define IDS_ASIND IDS_FNSZFIRST+4
#define IDS_ACOSD IDS_FNSZFIRST+5
#define IDS_ATAND IDS_FNSZFIRST+6
#define IDS_SINR IDS_FNSZFIRST+7
#define IDS_COSR IDS_FNSZFIRST+8
#define IDS_TANR IDS_FNSZFIRST+9
#define IDS_ASINR IDS_FNSZFIRST+10
#define IDS_ACOSR IDS_FNSZFIRST+11
#define IDS_ATANR IDS_FNSZFIRST+12
#define IDS_SING IDS_FNSZFIRST+13
#define IDS_COSG IDS_FNSZFIRST+14
#define IDS_TANG IDS_FNSZFIRST+15
#define IDS_ASING IDS_FNSZFIRST+16
#define IDS_ACOSG IDS_FNSZFIRST+17
#define IDS_ATANG IDS_FNSZFIRST+18
#define IDS_ASINH IDS_FNSZFIRST+19
#define IDS_ACOSH IDS_FNSZFIRST+20
#define IDS_ATANH IDS_FNSZFIRST+21
#define IDS_POWE IDS_FNSZFIRST+22
#define IDS_POW10 IDS_FNSZFIRST+23
#define IDS_SQRT IDS_FNSZFIRST+24
#define IDS_SQR IDS_FNSZFIRST+25
#define IDS_CUBE IDS_FNSZFIRST+26
#define IDS_CUBERT IDS_FNSZFIRST+27
#define IDS_FACT IDS_FNSZFIRST+28
#define IDS_REC IDS_FNSZFIRST+29
#define IDS_DEGREES IDS_FNSZFIRST+30
#define IDS_NEGATE IDS_FNSZFIRST+31
#define IDS_RSH IDS_FNSZFIRST+32
#define IDS_FNSZLAST IDS_RSH
#define IDS_ERRORS_FIRST IDS_FNSZLAST+1
#define IDS_ERRORS_FIRST 99
// This is the list of error strings corresponding to SCERR_DIVIDEZERO..
@ -91,7 +42,7 @@
#define IDS_ERR_INPUT_OVERFLOW CSTRINGSENGMAX+9
#define IDS_ERR_OUTPUT_OVERFLOW CSTRINGSENGMAX+10
// Resource keys for CEngineStrings.resw
#define SIDS_PLUS_MINUS L"0"
#define SIDS_CLEAR L"1"
#define SIDS_CE L"2"
@ -214,7 +165,8 @@
#define SIDS_ERR_INPUT_OVERFLOW L"119"
#define SIDS_ERR_OUTPUT_OVERFLOW L"120"
__declspec(selectany) std::wstring g_sids[] =
// Include the resource key ID from above into this vector to load it into memory for the engine to use
__declspec(selectany) std::vector<std::wstring> g_sids =
{
std::wstring(SIDS_PLUS_MINUS),
std::wstring(SIDS_C),