76 lines
2.7 KiB
C++
76 lines
2.7 KiB
C++
#ifndef PROCESS_CONTROLLER_PREDICATE_HPP
|
|
#define PROCESS_CONTROLLER_PREDICATE_HPP
|
|
|
|
#include "process_definition.hpp" // TODO: generate to <build>/idl/process/process_definition.hpp
|
|
|
|
#include <string>
|
|
#include <functional>
|
|
#include <type_traits>
|
|
|
|
namespace process::controller {
|
|
namespace predicate {
|
|
/**
|
|
* combines two predicates into one using a given binary operation
|
|
*/
|
|
template <typename P1, typename P2, typename Op>
|
|
[[nodiscard]] inline auto combine(const P1& p1, const P2& p2, const Op op) noexcept -> std::function<bool(const ProcessDefinition&)>
|
|
{
|
|
static_assert(std::is_invocable_r_v<bool, decltype(op), bool, bool>);
|
|
static_assert(std::is_invocable_r_v<bool, decltype(p1), const ProcessDefinition&>);
|
|
static_assert(std::is_invocable_r_v<bool, decltype(p2), const ProcessDefinition&>);
|
|
return [p1, p2, op](const ProcessDefinition& arg) noexcept -> bool {
|
|
static_assert(std::is_invocable_r_v<bool, decltype(op), bool, bool>);
|
|
static_assert(std::is_invocable_r_v<bool, decltype(p1), const ProcessDefinition&>);
|
|
static_assert(std::is_invocable_r_v<bool, decltype(p2), const ProcessDefinition&>);
|
|
return op(p1(arg), p2(arg));
|
|
};
|
|
}
|
|
|
|
/**
|
|
* combines two predicates using logical or
|
|
*
|
|
* returns a function operating on the same argument as p1 and p2
|
|
*/
|
|
template <typename P1, typename P2>
|
|
[[nodiscard]] inline auto Or(const P1& p1, const P2& p2) noexcept -> std::function<bool(const ProcessDefinition&)>
|
|
{
|
|
return combine(p1, p2, std::logical_or {});
|
|
}
|
|
|
|
/**
|
|
* equivalent to `const true`
|
|
*/
|
|
[[nodiscard]] constexpr bool allow_all(const ProcessDefinition&) noexcept { return true; }
|
|
|
|
/**
|
|
* returns a function that checks whether the node of a ProcessDefinition equals a given string
|
|
*/
|
|
[[nodiscard]] inline std::function<bool(const ProcessDefinition&)> node_equals(const std::string& node) noexcept
|
|
{
|
|
return [node](const ProcessDefinition& pd) noexcept -> bool {
|
|
return pd.node() == node;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* returns true iff the node specifies "any node", i.e. '*'
|
|
*/
|
|
[[nodiscard]] inline std::function<bool(const ProcessDefinition&)> is_any_node() noexcept
|
|
{
|
|
return node_equals("*");
|
|
}
|
|
|
|
/**
|
|
* returns a function that checks whether the node of a ProcessDefinition matches a given pattern
|
|
*
|
|
* TODO implement more sophisticated glob matching, we currently support '*' and exact match
|
|
*/
|
|
[[nodiscard]] inline std::function<bool(const ProcessDefinition&)> node_matches(const std::string& pattern) noexcept
|
|
{
|
|
return Or(is_any_node(), node_equals(pattern));
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endif /* PROCESS_CONTROLLER_PREDICATE_HPP */
|