73 lines
1.7 KiB
C++
73 lines
1.7 KiB
C++
#pragma once
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <functional>
|
|
#include <limits>
|
|
#include <map>
|
|
#include <vector>
|
|
namespace FunsObj {
|
|
|
|
using namespace std::chrono;
|
|
using std::chrono::system_clock;
|
|
using TimePoint = system_clock::time_point;
|
|
using TimeDur = milliseconds;
|
|
|
|
struct Keep {
|
|
template <typename T>
|
|
bool operator()(T value, T seconds) {
|
|
last_value_ = now_value_;
|
|
now_value_ = value;
|
|
if (fabs(now_value_ - last_value_) >
|
|
std::numeric_limits<double>::epsilon()) {
|
|
is_keep_ = false;
|
|
last_time_ = std::chrono::system_clock::now();
|
|
} else {
|
|
if (std::chrono::system_clock::now() - last_time_ >
|
|
std::chrono::milliseconds(int64_t(seconds * 1000))) {
|
|
is_keep_ = true;
|
|
} else {
|
|
is_keep_ = false;
|
|
}
|
|
}
|
|
return is_keep_;
|
|
}
|
|
|
|
private:
|
|
TimePoint last_time_ = std::chrono::system_clock::now();
|
|
double last_value_ = 0;
|
|
double now_value_ = 0;
|
|
bool is_keep_ = false;
|
|
};
|
|
|
|
template <typename T>
|
|
bool gitbit(T value, T sub) {
|
|
return int(value) >> int(sub) & 1;
|
|
}
|
|
|
|
using Two = std::function<bool(double, double)>;
|
|
|
|
const std::map<std::string, Two> TwoFuns = {
|
|
{"Keep", FunsObj::Keep()}, {"GitBit", FunsObj::gitbit<double>}};
|
|
|
|
struct AdapterParam {
|
|
std::string type;
|
|
std::string name;
|
|
int index;
|
|
};
|
|
|
|
struct HandleTwo {
|
|
bool operator()(AdapterParam adapter_param, double p1, double p2) {
|
|
std::string funs_id = adapter_param.type + adapter_param.name +
|
|
std::to_string(adapter_param.index);
|
|
if (twos.find(funs_id) == twos.end()) {
|
|
twos[funs_id] = TwoFuns.at(adapter_param.type);
|
|
}
|
|
return twos[funs_id](p1, p2);
|
|
}
|
|
|
|
private:
|
|
std::map<std::string, Two> twos;
|
|
};
|
|
|
|
} // namespace FunsObj
|