Skip to content

Commit

Permalink
Removed the void * argument from the lambda rules. Adds signals.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvanhamme committed Jul 5, 2020
1 parent e5152b9 commit 2ad3f54
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
22 changes: 6 additions & 16 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,15 @@ test_action_interface_t::~test_action_interface_t()
{
}

bool if_function(void *arg)
bool if_function()
{
int *a = reinterpret_cast<int *>(arg);
if (a == nullptr) {
return true;
}
std::cout << "IF_FUNCTION! a=" << *a << std::endl;
std::cout << "IF_FUNCTION!" << std::endl;
return false;
}

bool else_function(void *arg)
bool else_function()
{
int *a = reinterpret_cast<int *>(arg);
if (a == nullptr) {
return true;
}
std::cout << "ELSE_FUNCTION! a=" << *a << std::endl;
std::cout << "ELSE_FUNCTION!" << std::endl;
return false;
}

Expand Down Expand Up @@ -244,16 +236,14 @@ int main()
// conditions.push_back(parser.parse(lexer));
// }

std::string text = "on created if title contains \"Alacritty\" & property_a equals 4";
std::string text = "on fullscreened if title contains \"Alacritty\" & property_a equals 4";
wf::lambda_rule_parser_t parser;

auto rule = parser.parse(text, &if_function, &else_function);

std::cout << "rule: " << rule->to_string() << std::endl;

int a = 14;

auto error = rule->apply("created", access_interface, &a);
auto error = rule->apply("fullscreened", access_interface);
if (error)
{
std::cout << "Error!" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion wayfire/lexer/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace wf
/**
* @brief Set of all the signals recognized by the lexer.
*/
static const std::set<std::string_view> SIGNALS = {"created"};
static const std::set<std::string_view> SIGNALS = {"created", "maximized", "minimized", "fullscreened"};

/**
* @brief Set of all the keywords recognized by the lexer.
Expand Down
6 changes: 3 additions & 3 deletions wayfire/rule/lambda_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void lambda_rule_t::setElseLambda(lambda_t else_lambda)
_else_lambda = else_lambda;
}

bool lambda_rule_t::apply(const std::string &signal, access_interface_t &access, void *argument)
bool lambda_rule_t::apply(const std::string &signal, access_interface_t &access)
{
if ((signal.empty()) || (_condition == nullptr) || (_if_lambda == nullptr))
{
Expand All @@ -39,13 +39,13 @@ bool lambda_rule_t::apply(const std::string &signal, access_interface_t &access,
{
if (check_result)
{
error = _if_lambda(argument);
error = _if_lambda();
}
else
{
if (_else_lambda != nullptr)
{
error = _else_lambda(argument);
error = _else_lambda();
}
}

Expand Down
7 changes: 3 additions & 4 deletions wayfire/rule/lambda_rule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class access_interface_t;
* @note The lambda function executed by this rule as if or else case returns a boolean which is
* <code>true</code> if there was an error. <code>False</code> if no error.
*/
using lambda_t = std::function<bool(void *)>;
using lambda_t = std::function<bool()>;

/**
* @brief The lambda_rule_t class is a rule (combination of condition and action lambdass).
Expand Down Expand Up @@ -47,19 +47,18 @@ class lambda_rule_t

/**
* @brief apply Applies the rule to the given trigger signal. The interface access_interface_t is used to
* obtain data. The argument void pointer is passed to the if_lambda and else_lambda.
* obtain data.
*
* The rule will not do anything if the signal does not match the value of _signal. It is implemented this
* way to allow a set of rules to be 'applied' on all signals, and only have the relevant rules execute.
* Think of this as a built in signal filter.
*
* @param[in] signal The signal to apply the rule to.
* @param[in] access Acessor interface for the condition checks.
* @param[in] argument Argument pointer, passed to the lambdas if they execute.
*
* @return <code>True</code> if there was an error, <code>false</code> if not.
*/
bool apply(const std::string &signal, access_interface_t &access, void *argument = nullptr);
bool apply(const std::string &signal, access_interface_t &access);

/**
* @brief to_string Generates a string representation of the rule. Useful for debugging.
Expand Down

0 comments on commit 2ad3f54

Please sign in to comment.