2016-06-15 23:48:41 +02:00
|
|
|
#ifndef RANGEMATCHER_H_
|
|
|
|
#define RANGEMATCHER_H_
|
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "SerializeableMatcherIF.h"
|
|
|
|
#include "../../serialize/SerializeAdapter.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class RangeMatcher: public SerializeableMatcherIF<T> {
|
|
|
|
public:
|
|
|
|
bool inverted;
|
|
|
|
T lowerBound;
|
|
|
|
T upperBound;
|
|
|
|
|
|
|
|
RangeMatcher() :
|
|
|
|
inverted(true), lowerBound(1), upperBound(0) {
|
|
|
|
}
|
|
|
|
RangeMatcher(T lowerBound, T upperBound, bool inverted = false) :
|
|
|
|
inverted(inverted), lowerBound(lowerBound), upperBound(upperBound) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool match(T input) {
|
|
|
|
if (inverted) {
|
2018-07-12 16:29:32 +02:00
|
|
|
return !doMatch(input);
|
2016-06-15 23:48:41 +02:00
|
|
|
} else {
|
|
|
|
return doMatch(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
ReturnValue_t serialize(uint8_t **buffer, size_t *size, size_t maxSize,
|
2020-04-21 21:34:03 +02:00
|
|
|
SerializeIF::Endianness streamEndianness) const override {
|
|
|
|
ReturnValue_t result = SerializeAdapter::serialize(&lowerBound, buffer,
|
2020-04-21 22:28:43 +02:00
|
|
|
size, maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::serialize(&upperBound, buffer, size,
|
2020-04-21 22:28:43 +02:00
|
|
|
maxSize, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 22:28:43 +02:00
|
|
|
return SerializeAdapter::serialize(&inverted, buffer, size, maxSize,
|
2020-04-21 21:34:03 +02:00
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 22:28:43 +02:00
|
|
|
size_t getSerializedSize() const override {
|
2016-06-15 23:48:41 +02:00
|
|
|
return sizeof(lowerBound) + sizeof(upperBound) + sizeof(bool);
|
|
|
|
}
|
|
|
|
|
2020-04-21 21:34:03 +02:00
|
|
|
ReturnValue_t deSerialize(const uint8_t **buffer, size_t *size,
|
|
|
|
SerializeIF::Endianness streamEndianness) override {
|
|
|
|
ReturnValue_t result = SerializeAdapter::deSerialize(&lowerBound,
|
|
|
|
buffer, size, streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 21:34:03 +02:00
|
|
|
result = SerializeAdapter::deSerialize(&upperBound, buffer, size,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 21:34:03 +02:00
|
|
|
return SerializeAdapter::deSerialize(&inverted, buffer, size,
|
|
|
|
streamEndianness);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
protected:
|
|
|
|
bool doMatch(T input) {
|
|
|
|
return (input >= lowerBound) && (input <= upperBound);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* RANGEMATCHER_H_ */
|