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
|
#include "math-helpers.hpp"
#include "obs-module-helper.hpp"
#include <climits>
#include <exprtk.hpp>
#include <random>
namespace advss {
std::variant<double, std::string> EvalMathExpression(const std::string &expr)
{
static bool setupDone = false;
static exprtk::symbol_table<double> symbolTable;
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_real_distribution<double> dis(0.0, 1.0);
static auto randomFunc = []() {
return dis(gen);
};
if (!setupDone) {
symbolTable.add_function("random", randomFunc);
setupDone = true;
}
exprtk::expression<double> expression;
expression.register_symbol_table(symbolTable);
exprtk::parser<double> parser;
if (parser.compile(expr, expression)) {
return expression.value();
}
return std::string(obs_module_text(
"AdvSceneSwitcher.math.expressionFail")) +
" \"" + expr + "\"";
}
bool IsValidNumber(const std::string &str)
{
return GetDouble(str).has_value();
}
std::optional<double> GetDouble(const std::string &str)
{
char *end = nullptr;
double value = std::strtod(str.c_str(), &end);
if (end != str.c_str() && *end == '\0' && value != HUGE_VAL &&
value != -HUGE_VAL) {
return value;
}
return {};
}
std::optional<int> GetInt(const std::string &str)
{
char *end = nullptr;
int value = (int)std::strtol(str.c_str(), &end, 10);
if (end != str.c_str() && *end == '\0' && value != INT_MAX &&
value != INT_MIN) {
return value;
}
return {};
}
bool DoubleEquals(double left, double right, double epsilon)
{
return (fabs(left - right) < epsilon);
}
} // namespace advss
|