Move MathParser to Common

This commit is contained in:
Birunthan Mohanathas 2013-06-13 17:00:24 +03:00
parent 3cebbc6b53
commit be3efa7531
9 changed files with 28 additions and 28 deletions

View File

@ -22,6 +22,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="ControlTemplate.cpp" /> <ClCompile Include="ControlTemplate.cpp" />
<ClCompile Include="Dialog.cpp" /> <ClCompile Include="Dialog.cpp" />
<ClCompile Include="MathParser.cpp" />
<ClCompile Include="MenuTemplate.cpp" /> <ClCompile Include="MenuTemplate.cpp" />
<ClCompile Include="PathUtil.cpp" /> <ClCompile Include="PathUtil.cpp" />
<ClCompile Include="Platform.cpp" /> <ClCompile Include="Platform.cpp" />
@ -30,6 +31,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="ControlTemplate.h" /> <ClInclude Include="ControlTemplate.h" />
<ClInclude Include="Dialog.h" /> <ClInclude Include="Dialog.h" />
<ClInclude Include="MathParser.h" />
<ClInclude Include="MenuTemplate.h" /> <ClInclude Include="MenuTemplate.h" />
<ClInclude Include="PathUtil.h" /> <ClInclude Include="PathUtil.h" />
<ClInclude Include="Platform.h" /> <ClInclude Include="Platform.h" />

View File

@ -7,6 +7,7 @@
<ClCompile Include="Platform.cpp" /> <ClCompile Include="Platform.cpp" />
<ClCompile Include="StringUtil.cpp" /> <ClCompile Include="StringUtil.cpp" />
<ClCompile Include="ControlTemplate.cpp" /> <ClCompile Include="ControlTemplate.cpp" />
<ClCompile Include="MathParser.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Dialog.h" /> <ClInclude Include="Dialog.h" />
@ -16,5 +17,6 @@
<ClInclude Include="RawString.h" /> <ClInclude Include="RawString.h" />
<ClInclude Include="StringUtil.h" /> <ClInclude Include="StringUtil.h" />
<ClInclude Include="ControlTemplate.h" /> <ClInclude Include="ControlTemplate.h" />
<ClInclude Include="MathParser.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -18,8 +18,8 @@
// Heavily based on ccalc 0.5.1 by Walery Studennikov <hqsoftware@mail.ru> // Heavily based on ccalc 0.5.1 by Walery Studennikov <hqsoftware@mail.ru>
#include "StdAfx.h" #include <string>
#include "MeasureCalc.h" #include <stdint.h>
#include "MathParser.h" #include "MathParser.h"
static const double M_E = 2.7182818284590452354; static const double M_E = 2.7182818284590452354;
@ -242,12 +242,13 @@ const WCHAR* MathParser::CheckedParse(const WCHAR* formula, double* result)
const WCHAR* error = Check(formula); const WCHAR* error = Check(formula);
if (!error) if (!error)
{ {
error = Parse(formula, nullptr, result); error = Parse(formula, result);
} }
return error; return error;
} }
const WCHAR* MathParser::Parse(const WCHAR* formula, MeasureCalc* calc, double* result) const WCHAR* MathParser::Parse(
const WCHAR* formula, double* result, GetValueFunc getValue, void* getValueContext)
{ {
static WCHAR errorBuffer[128]; static WCHAR errorBuffer[128];
@ -400,7 +401,7 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, MeasureCalc* calc, double*
else else
{ {
double dblval; double dblval;
if (calc && calc->GetMeasureValue(lexer.name, lexer.nameLen, &dblval)) if (getValue && getValue(lexer.name, lexer.nameLen, &dblval, getValueContext))
{ {
parser.numStack[++parser.valTop] = dblval; parser.numStack[++parser.valTop] = dblval;
break; break;

View File

@ -18,16 +18,20 @@
// Heavily based on ccalc 0.5.1 by Walery Studennikov <hqsoftware@mail.ru> // Heavily based on ccalc 0.5.1 by Walery Studennikov <hqsoftware@mail.ru>
#ifndef __MATHPARSER_H__ #ifndef RM_COMMON_MATHPARSER_H_
#define __MATHPARSER_H__ #define RM_COMMON_MATHPARSER_H_
class MeasureCalc; #include <Windows.h>
namespace MathParser namespace MathParser
{ {
typedef bool (*GetValueFunc)(const WCHAR* str, int len, double* value, void* context);
const WCHAR* Check(const WCHAR* formula); const WCHAR* Check(const WCHAR* formula);
const WCHAR* CheckedParse(const WCHAR* formula, double* result); const WCHAR* CheckedParse(const WCHAR* formula, double* result);
const WCHAR* Parse(const WCHAR* formula, MeasureCalc* calc, double* result); const WCHAR* Parse(
const WCHAR* formula, double* result,
GetValueFunc getValue = nullptr, void* getValueContext = nullptr);
bool IsDelimiter(WCHAR ch); bool IsDelimiter(WCHAR ch);
}; };

View File

@ -17,9 +17,9 @@
*/ */
#include "StdAfx.h" #include "StdAfx.h"
#include "../Common/MathParser.h"
#include "../Common/PathUtil.h" #include "../Common/PathUtil.h"
#include "ConfigParser.h" #include "ConfigParser.h"
#include "MathParser.h"
#include "Litestep.h" #include "Litestep.h"
#include "Rainmeter.h" #include "Rainmeter.h"
#include "System.h" #include "System.h"

View File

@ -102,9 +102,6 @@
<ClCompile Include="Logger.cpp"> <ClCompile Include="Logger.cpp">
<PrecompiledHeader>Use</PrecompiledHeader> <PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile> </ClCompile>
<ClCompile Include="MathParser.cpp">
<PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Measure.cpp"> <ClCompile Include="Measure.cpp">
<PrecompiledHeader>Use</PrecompiledHeader> <PrecompiledHeader>Use</PrecompiledHeader>
</ClCompile> </ClCompile>
@ -316,7 +313,6 @@
<ClInclude Include="Litestep.h" /> <ClInclude Include="Litestep.h" />
<ClInclude Include="DialogManage.h" /> <ClInclude Include="DialogManage.h" />
<ClInclude Include="Logger.h" /> <ClInclude Include="Logger.h" />
<ClInclude Include="MathParser.h" />
<ClInclude Include="Measure.h" /> <ClInclude Include="Measure.h" />
<ClInclude Include="MeasureCalc.h" /> <ClInclude Include="MeasureCalc.h" />
<ClInclude Include="MeasureCPU.h" /> <ClInclude Include="MeasureCPU.h" />

View File

@ -321,9 +321,6 @@
<ClCompile Include="Export.cpp"> <ClCompile Include="Export.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="MathParser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Mouse.cpp"> <ClCompile Include="Mouse.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -596,9 +593,6 @@
<ClInclude Include="Export.h"> <ClInclude Include="Export.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="MathParser.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lua\lua\luaconf.h"> <ClInclude Include="lua\lua\luaconf.h">
<Filter>Lua\Lua</Filter> <Filter>Lua\Lua</Filter>
</ClInclude> </ClInclude>

View File

@ -17,9 +17,9 @@
*/ */
#include "StdAfx.h" #include "StdAfx.h"
#include "../Common/MathParser.h"
#include "MeasureCalc.h" #include "MeasureCalc.h"
#include "Rainmeter.h" #include "Rainmeter.h"
#include "MathParser.h"
bool MeasureCalc::c_RandSeeded = false; bool MeasureCalc::c_RandSeeded = false;
@ -56,7 +56,7 @@ MeasureCalc::~MeasureCalc()
*/ */
void MeasureCalc::UpdateValue() void MeasureCalc::UpdateValue()
{ {
const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), this, &m_Value); const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), &m_Value, GetMeasureValue, this);
if (errMsg != nullptr) if (errMsg != nullptr)
{ {
if (!m_ParseError) if (!m_ParseError)
@ -145,9 +145,10 @@ void MeasureCalc::FormulaReplace()
while (pos != std::wstring::npos); while (pos != std::wstring::npos);
} }
bool MeasureCalc::GetMeasureValue(const WCHAR* str, int len, double* value) bool MeasureCalc::GetMeasureValue(const WCHAR* str, int len, double* value, void* context)
{ {
const std::vector<Measure*>& measures = m_MeterWindow->GetMeasures(); auto calc = (MeasureCalc*)context;
const std::vector<Measure*>& measures = calc->m_MeterWindow->GetMeasures();
std::vector<Measure*>::const_iterator iter = measures.begin(); std::vector<Measure*>::const_iterator iter = measures.begin();
for ( ; iter != measures.end(); ++iter) for ( ; iter != measures.end(); ++iter)
@ -162,12 +163,12 @@ bool MeasureCalc::GetMeasureValue(const WCHAR* str, int len, double* value)
if (_wcsnicmp(str, L"counter", len) == 0) if (_wcsnicmp(str, L"counter", len) == 0)
{ {
*value = m_MeterWindow->GetUpdateCounter(); *value = calc->m_MeterWindow->GetUpdateCounter();
return true; return true;
} }
else if (_wcsnicmp(str, L"random", len) == 0) else if (_wcsnicmp(str, L"random", len) == 0)
{ {
*value = GetRandom(); *value = calc->GetRandom();
return true; return true;
} }

View File

@ -29,13 +29,13 @@ public:
virtual UINT GetTypeID() { return TypeID<MeasureCalc>(); } virtual UINT GetTypeID() { return TypeID<MeasureCalc>(); }
bool GetMeasureValue(const WCHAR* str, int len, double* value);
protected: protected:
virtual void ReadOptions(ConfigParser& parser, const WCHAR* section); virtual void ReadOptions(ConfigParser& parser, const WCHAR* section);
virtual void UpdateValue(); virtual void UpdateValue();
private: private:
static bool GetMeasureValue(const WCHAR* str, int len, double* value, void* context);
void FormulaReplace(); void FormulaReplace();
int GetRandom(); int GetRandom();