JaffarPlus
High-performance best-first search optimizer for tool-assisted speedruns
Loading...
Searching...
No Matches
condition.hpp
Go to the documentation of this file.
1#pragma once
2
9#include "property.hpp"
10#include <jaffarCommon/bitwise.hpp>
11
12namespace jaffarPlus
13{
14
23{
24public:
39
44 Condition(const operator_t opType) : _opType(opType) {}
45
50 virtual __INLINE__ bool evaluate() const = 0;
51 virtual ~Condition() = default;
52
59 static __INLINE__ operator_t getOperatorType(const std::string& operation)
60 {
61 if (operation == "==") return op_equal;
62 if (operation == "!=") return op_not_equal;
63 if (operation == ">") return op_greater;
64 if (operation == ">=") return op_greater_or_equal;
65 if (operation == "<") return op_less;
66 if (operation == "<=") return op_less_or_equal;
67 if (operation == "%0") return op_modulo_zero;
68 if (operation == "%N") return op_modulo_non_zero;
69 if (operation == "BitTrue") return op_bit_true;
70 if (operation == "BitFalse") return op_bit_false;
71
72 JAFFAR_THROW_LOGIC("[Error] Unrecognized operator: '%s'. Valid operators are: ==, !=, >, >=, <, <=, %%0, %%N, BitTrue, BitFalse\n", operation.c_str());
73
74 return op_equal;
75 }
76
77protected:
80};
81
91template <typename T>
92class _vCondition : public Condition
93{
94public:
103 _vCondition(const operator_t opType, Property* property1, Property* property2, T immediate1, T immediate2)
104 : Condition(opType), _property1(property1), _property2(property2), _immediate1(immediate1), _immediate2(immediate2)
105 {
106 switch (_opType)
107 {
108 case op_equal: _opFcPtr = _opEqual; break;
109 case op_not_equal: _opFcPtr = _opNotEqual; break;
110 case op_greater: _opFcPtr = _opGreater; break;
112 case op_less: _opFcPtr = _opLess; break;
114 case op_bit_true: _opFcPtr = _opBitTrue; break;
115 case op_bit_false: _opFcPtr = _opBitFalse; break;
118 default: break;
119 }
120 }
121
129 __INLINE__ bool evaluate() const
130 {
131 T immediate1 = _immediate1;
132 T immediate2 = _immediate2;
133 if (_property1 != nullptr) immediate1 = _property1->getValue<T>();
134 if (_property2 != nullptr) immediate2 = _property2->getValue<T>();
135 return _opFcPtr(immediate1, immediate2);
136 }
137
138private:
140 static __INLINE__ bool _opEqual(const T a, const T b) { return a == b; }
142 static __INLINE__ bool _opNotEqual(const T a, const T b) { return a != b; }
144 static __INLINE__ bool _opGreater(const T a, const T b) { return a > b; }
146 static __INLINE__ bool _opGreaterOrEqual(const T a, const T b) { return a >= b; }
148 static __INLINE__ bool _opLess(const T a, const T b) { return a < b; }
150 static __INLINE__ bool _opLessOrEqual(const T a, const T b) { return a <= b; }
152 static __INLINE__ bool _opBitTrue(const T a, const T b) { return jaffarCommon::bitwise::getBitFlag(a, b); }
154 static __INLINE__ bool _opBitFalse(const T a, const T b) { return !jaffarCommon::bitwise::getBitFlag(a, b); }
156 static __INLINE__ bool _opModuloZero(const T a, const T b) { return (uint64_t)a % (uint64_t)b == 0; }
158 static __INLINE__ bool _opModuloNonZero(const T a, const T b) { return (uint64_t)a % (uint64_t)b > 0; }
159
161 bool (*_opFcPtr)(const T, const T);
162
165 const T _immediate1;
166 const T _immediate2;
167};
168
169} // namespace jaffarPlus
Abstract base for a single boolean comparison.
Definition condition.hpp:23
virtual bool evaluate() const =0
Evaluates the condition against the current values of its operands.
const operator_t _opType
The comparison operator selected for this condition.
Definition condition.hpp:79
static operator_t getOperatorType(const std::string &operation)
Maps a configuration operator string to its operator_t value.
Definition condition.hpp:59
operator_t
The comparison operator applied between the condition's two operands.
Definition condition.hpp:27
@ op_bit_false
the bit of operand1 at index operand2 is clear (config operator "BitFalse")
Definition condition.hpp:35
@ op_equal
operand1 == operand2 (config operator "==")
Definition condition.hpp:28
@ op_bit_true
the bit of operand1 at index operand2 is set (config operator "BitTrue")
Definition condition.hpp:34
@ op_modulo_non_zero
operand1 % operand2 != 0 (config operator "%N")
Definition condition.hpp:37
@ op_not_equal
operand1 != operand2 (config operator "!=")
Definition condition.hpp:29
@ op_modulo_zero
operand1 % operand2 == 0 (config operator "%0")
Definition condition.hpp:36
@ op_greater
operand1 > operand2 (config operator ">")
Definition condition.hpp:30
@ op_less
operand1 < operand2 (config operator "<")
Definition condition.hpp:32
@ op_less_or_equal
operand1 <= operand2 (config operator "<=")
Definition condition.hpp:33
@ op_greater_or_equal
operand1 >= operand2 (config operator ">=")
Definition condition.hpp:31
Condition(const operator_t opType)
Constructs a condition with the given operator.
Definition condition.hpp:44
A named, typed reference to a value stored at a memory address.
Definition property.hpp:25
T getValue() const
Reads the property's value at its memory address as type T, applying endianness conversion.
Definition property.hpp:144
Concrete, type-specialized condition over operands of type T.
Definition condition.hpp:93
_vCondition(const operator_t opType, Property *property1, Property *property2, T immediate1, T immediate2)
Builds a typed condition from its operands.
const T _immediate2
Second operand's immediate value (used when _property2 is null).
static bool _opLessOrEqual(const T a, const T b)
Returns a <= b.
static bool _opBitTrue(const T a, const T b)
Returns true if the bit of a at index b is set.
static bool _opGreaterOrEqual(const T a, const T b)
Returns a >= b.
static bool _opModuloNonZero(const T a, const T b)
Returns true if a % b != 0.
const T _immediate1
First operand's immediate value (used when _property1 is null).
Property *const _property2
Second operand as a property, or nullptr to use _immediate2.
static bool _opLess(const T a, const T b)
Returns a < b.
Property *const _property1
First operand as a property, or nullptr to use _immediate1.
static bool _opGreater(const T a, const T b)
Returns a > b.
bool(* _opFcPtr)(const T, const T)
Resolved operator implementation, selected once at construction from _opType.
static bool _opBitFalse(const T a, const T b)
Returns true if the bit of a at index b is clear.
static bool _opEqual(const T a, const T b)
Returns a == b.
bool evaluate() const
Evaluates the comparison using the operands' current values.
static bool _opNotEqual(const T a, const T b)
Returns a != b.
static bool _opModuloZero(const T a, const T b)
Returns true if a % b == 0.
A named, typed view into a region of game memory, with datatype and endianness handling and a templat...