Simplify some of the calc engine string logic (#449)

Description of the changes:
Currently Calculator handles strings by defining integers for each type of function that can be performed, this integer will eventually correspond with an index in s_engineStrings which holds the corresponding display string for each function. Some functions such as Sin can have multiple strings (degrees, rads, grads, inverse). Functions like Sin are mapped to another array called "rgUfne" where a new integer is given depending on the output string which will then be given to s_engineStrings. The new integer returned by the "rgUfne" array runs the risk of overlapping with any new functions that may be added in CCommand.h. Furthermore, it is expected that the strings in s_engineStrings and rgUfne are defined in a particular order (not necessarily sequential), otherwise the logic will break. This makes adding new strings for new functions confusing and difficult, since a lot of the logic is not clearly defined.

This PR attempts to make this a bit simpler by changing the s_engineStrings and rgUfne arrays to be unordered_maps instead of arrays. For s_engineStrings the keys will now be strings, allowing the existing logic for indexing to be used by simply converting the number into a string to access the value. This will also allow us to create keys in the future that are not limited to integers but to strings that hold more meaning.

The rgUfne array will also be updated to be a map that will take in an integer and give you the corresponding string that can be passed to s_engineStrings. The UFNE object in the rgUfne array will also be updated to hold all the possible string keys for a function, instead of indexing them on other numbers that may overlap with existing definitions.

Now to add a new string for a new IDC_FOO function, we would just need to add the "FooString" resource keys to the g_sids array and use the updated rgUfne map to link the IDC_FOO value to the corresponding "FooString" resource key. This way the resource key can be a meaningful string, and not an integer that must be in any particular order.

How changes were validated:
Tested each function manually in standard, scientific, and programmer modes.
This commit is contained in:
Pepe Rivera
2019-04-11 15:20:01 -07:00
committed by Daniel Belcher
parent 47a2741218
commit f6f10444f7
5 changed files with 348 additions and 459 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 (const auto& sid : g_sids)
{
s_engineStrings[i] = resourceProvider.GetCEngineString(g_sids[i]);
auto locKey = wstring{ sid };
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;
@@ -868,155 +861,94 @@ void CCalcEngine::DisplayAnnounceBinaryOperator()
// Unary operator Function Name table Element
// since unary operators button names aren't exactly friendly for history purpose,
// we have this separate table to get its localized name and for its Inv function if it exists.
typedef struct
struct FunctionNameElement
{
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
} UFNE;
wstring degreeString; // Used by default if there are no rad or grad specific strings.
wstring inverseDegreeString; // Will fall back to degreeString if empty
wstring radString;
wstring inverseRadString; // Will fall back to radString if empty
wstring gradString;
wstring inverseGradString; // Will fall back to gradString if empty
bool hasAngleStrings = ((!radString.empty()) || (!inverseRadString.empty()) || (!gradString.empty()) || (!inverseGradString.empty()));
};
// Table for each unary operator
static const UFNE rgUfne[] =
static const std::unordered_map<int, FunctionNameElement> 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_RECIPROC } },
{ 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)
{
return GetString(IDS_NEGATE);
}
if (IDC_DEGREES == nOpCode)
{
return GetString(IDS_DEGREES);
}
// Correct the trigonometric functions with type of angle argument they take
if (ANGLE_RAD == angletype)
{
switch (nOpCode)
{
case IDC_SIN:
nOpCode = IDC_RADSIN;
break;
case IDC_COS:
nOpCode = IDC_RADCOS;
break;
case IDC_TAN:
nOpCode = IDC_RADTAN;
break;
}
}
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))
wstring ids = L"";
if (auto pair = unaryOperatorStringTable.find(nOpCode); pair != unaryOperatorStringTable.end())
{
if (fInv)
const FunctionNameElement& element = pair->second;
if (!element.hasAngleStrings || ANGLE_DEG == angletype)
{
ids = rgUfne[iufne].idsFuncInv;
if (fInv)
{
ids = element.inverseDegreeString;
}
if (ids.empty())
{
ids = element.degreeString;
}
}
if (0 == ids)
else if (ANGLE_RAD == angletype)
{
ids = rgUfne[iufne].idsFunc;
if (fInv)
{
ids = element.inverseRadString;
}
if (ids.empty())
{
ids = element.radString;
}
}
else if (ANGLE_GRAD == angletype)
{
if (fInv)
{
ids = element.inverseGradString;
}
if (ids.empty())
{
ids = element.gradString;
}
}
}
if (!ids.empty())
{
return GetString(ids);
}
// If we didn't find an ID in the table, use the op code.
if (0 == ids)
{
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 OpCodeToString(nOpCode);
}
bool CCalcEngine::IsCurrentTooBigForTrig()