From be3efa753193c65183143990e883c9c0be69b7fa Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Thu, 13 Jun 2013 17:00:24 +0300 Subject: [PATCH] Move MathParser to Common --- Common/Common.vcxproj | 2 ++ Common/Common.vcxproj.filters | 2 ++ {Library => Common}/MathParser.cpp | 11 ++++++----- {Library => Common}/MathParser.h | 12 ++++++++---- Library/ConfigParser.cpp | 2 +- Library/Library.vcxproj | 4 ---- Library/Library.vcxproj.filters | 6 ------ Library/MeasureCalc.cpp | 13 +++++++------ Library/MeasureCalc.h | 4 ++-- 9 files changed, 28 insertions(+), 28 deletions(-) rename {Library => Common}/MathParser.cpp (93%) rename {Library => Common}/MathParser.h (74%) diff --git a/Common/Common.vcxproj b/Common/Common.vcxproj index 284a6bc5..97eeda9d 100644 --- a/Common/Common.vcxproj +++ b/Common/Common.vcxproj @@ -22,6 +22,7 @@ + @@ -30,6 +31,7 @@ + diff --git a/Common/Common.vcxproj.filters b/Common/Common.vcxproj.filters index 8f9d982b..2f4f152e 100644 --- a/Common/Common.vcxproj.filters +++ b/Common/Common.vcxproj.filters @@ -7,6 +7,7 @@ + @@ -16,5 +17,6 @@ + \ No newline at end of file diff --git a/Library/MathParser.cpp b/Common/MathParser.cpp similarity index 93% rename from Library/MathParser.cpp rename to Common/MathParser.cpp index 5ed4bf39..a3ed6cbe 100644 --- a/Library/MathParser.cpp +++ b/Common/MathParser.cpp @@ -18,8 +18,8 @@ // Heavily based on ccalc 0.5.1 by Walery Studennikov -#include "StdAfx.h" -#include "MeasureCalc.h" +#include +#include #include "MathParser.h" 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); if (!error) { - error = Parse(formula, nullptr, result); + error = Parse(formula, result); } 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]; @@ -400,7 +401,7 @@ const WCHAR* MathParser::Parse(const WCHAR* formula, MeasureCalc* calc, double* else { 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; break; diff --git a/Library/MathParser.h b/Common/MathParser.h similarity index 74% rename from Library/MathParser.h rename to Common/MathParser.h index 328f0301..8ebf4dd3 100644 --- a/Library/MathParser.h +++ b/Common/MathParser.h @@ -18,16 +18,20 @@ // Heavily based on ccalc 0.5.1 by Walery Studennikov -#ifndef __MATHPARSER_H__ -#define __MATHPARSER_H__ +#ifndef RM_COMMON_MATHPARSER_H_ +#define RM_COMMON_MATHPARSER_H_ -class MeasureCalc; +#include namespace MathParser { + typedef bool (*GetValueFunc)(const WCHAR* str, int len, double* value, void* context); + const WCHAR* Check(const WCHAR* formula); 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); }; diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index c7cfb3a4..c2af5ce3 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -17,9 +17,9 @@ */ #include "StdAfx.h" +#include "../Common/MathParser.h" #include "../Common/PathUtil.h" #include "ConfigParser.h" -#include "MathParser.h" #include "Litestep.h" #include "Rainmeter.h" #include "System.h" diff --git a/Library/Library.vcxproj b/Library/Library.vcxproj index 972f6478..150e8f49 100644 --- a/Library/Library.vcxproj +++ b/Library/Library.vcxproj @@ -102,9 +102,6 @@ Use - - Use - Use @@ -316,7 +313,6 @@ - diff --git a/Library/Library.vcxproj.filters b/Library/Library.vcxproj.filters index e8bc7d8f..b7247f75 100644 --- a/Library/Library.vcxproj.filters +++ b/Library/Library.vcxproj.filters @@ -321,9 +321,6 @@ Source Files - - Source Files - Source Files @@ -596,9 +593,6 @@ Header Files - - Header Files - Lua\Lua diff --git a/Library/MeasureCalc.cpp b/Library/MeasureCalc.cpp index cce4a6e6..ca05a886 100644 --- a/Library/MeasureCalc.cpp +++ b/Library/MeasureCalc.cpp @@ -17,9 +17,9 @@ */ #include "StdAfx.h" +#include "../Common/MathParser.h" #include "MeasureCalc.h" #include "Rainmeter.h" -#include "MathParser.h" bool MeasureCalc::c_RandSeeded = false; @@ -56,7 +56,7 @@ MeasureCalc::~MeasureCalc() */ 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 (!m_ParseError) @@ -145,9 +145,10 @@ void MeasureCalc::FormulaReplace() 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& measures = m_MeterWindow->GetMeasures(); + auto calc = (MeasureCalc*)context; + const std::vector& measures = calc->m_MeterWindow->GetMeasures(); std::vector::const_iterator iter = measures.begin(); 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) { - *value = m_MeterWindow->GetUpdateCounter(); + *value = calc->m_MeterWindow->GetUpdateCounter(); return true; } else if (_wcsnicmp(str, L"random", len) == 0) { - *value = GetRandom(); + *value = calc->GetRandom(); return true; } diff --git a/Library/MeasureCalc.h b/Library/MeasureCalc.h index f7fbd928..7f176984 100644 --- a/Library/MeasureCalc.h +++ b/Library/MeasureCalc.h @@ -29,13 +29,13 @@ public: virtual UINT GetTypeID() { return TypeID(); } - bool GetMeasureValue(const WCHAR* str, int len, double* value); - protected: virtual void ReadOptions(ConfigParser& parser, const WCHAR* section); virtual void UpdateValue(); private: + static bool GetMeasureValue(const WCHAR* str, int len, double* value, void* context); + void FormulaReplace(); int GetRandom();