mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
The "core" macro conditions and actions have been extracted out to the "base" plugin. The library now mostly contains functionality which is required across all plugins and (e.g. definitions for macro segments). The goal is to reduce the complexity and cross-dependencies and group the source files in a better way. This should relsove the "library limit of 65535 objects exceeded" build issue occuring in some Windows build environments.
61 lines
1.4 KiB
C++
61 lines
1.4 KiB
C++
#include "math-helpers.hpp"
|
|
#include "obs-module-helper.hpp"
|
|
|
|
#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) {
|
|
return value;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
std::optional<int> GetInt(const std::string &str)
|
|
{
|
|
char *end = nullptr;
|
|
int value = std::strtol(str.c_str(), &end, 10);
|
|
if (end != str.c_str() && *end == '\0') {
|
|
return value;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
} // namespace advss
|