Browse Source

feat: demo of class member function pointer with template

legacy
Dnomd343 2 years ago
parent
commit
2e7cdeb519
  1. 7
      src/core/core.h
  2. 104
      src/fast_cal.cc

7
src/core/core.h

@ -9,8 +9,11 @@
class Core { class Core {
public: public:
typedef void (*release_t)(uint64_t, uint64_t);
void next_step(uint64_t code, uint64_t mask); void next_step(uint64_t code, uint64_t mask);
explicit Core(void (*release_func)(uint64_t, uint64_t)) : release(release_func) {} explicit Core(release_t release_func) : release(release_func) {}
private: private:
struct cache_t { struct cache_t {
@ -22,7 +25,7 @@ private:
int cache_size = 1; int cache_size = 1;
cache_t cache[16] = {0}; cache_t cache[16] = {0};
void (*release)(uint64_t, uint64_t); // release code and mask release_t release; // release code and mask
void move_1x1(uint64_t code, int addr); void move_1x1(uint64_t code, int addr);
void move_1x2(uint64_t code, int addr); void move_1x2(uint64_t code, int addr);

104
src/fast_cal.cc

@ -17,14 +17,91 @@ std::queue<fast_cal_t*> cache;
std::unordered_map<uint64_t, fast_cal_t> cases; std::unordered_map<uint64_t, fast_cal_t> cases;
//bool stop_flag; void add_global_demo(uint64_t code) {
std::cout << RawCode(code).dump_case() << std::endl;
}
void add_new_case(uint64_t code, uint64_t mask) { class FastCal;
class GlobalCal;
template<typename T>
class CoreDemo {
public:
int code{0};
// if (stop_flag) { typedef void (T::*release_t)(uint64_t);
// return;
// void next(release_t release_func) {
// release_func(1);
// release_func(2);
// release_func(3);
// ++code;
// } // }
void next(T *f, release_t release_func) {
(f->*release_func)(1);
// release_func(2);
// release_func(3);
++code;
}
};
class FastCal {
public:
int dat;
void add(uint64_t code) {
std::cout << RawCode(code).dump_case() << std::endl;
++dat;
}
void run() {
auto cd = CoreDemo<FastCal>();
// cd.next(add_global_demo);
// cd.next(this->*add);
cd.next(this, &FastCal::add);
++dat;
}
};
class GlobalCal {
public:
int dat;
void add(uint64_t code) {
std::cout << RawCode(code).dump_case() << std::endl;
++dat;
}
void run() {
auto cd = CoreDemo<GlobalCal>();
cd.next(this, &GlobalCal::add);
++dat;
}
};
//void (FastCal::*ptrStaticFun)(uint64_t) = &FastCal::add;
//release_t ptrFun = &FastCal::add;
//void demo() {
// auto f = new FastCal();
// (f->*ptrFun)(1);
//}
void add_new_case(uint64_t code, uint64_t mask) {
auto exist_case = cases.find(code); auto exist_case = cases.find(code);
if (exist_case != cases.end()) { // find existed case if (exist_case != cases.end()) { // find existed case
@ -36,7 +113,6 @@ void add_new_case(uint64_t code, uint64_t mask) {
cases[code] = fast_cal_t { cases[code] = fast_cal_t {
.code = code, .code = code,
.mask = mask, .mask = mask,
// .step = cache.front()->step + 1,
.last = cache.front(), .last = cache.front(),
};; };;
cache.emplace(&cases[code]); cache.emplace(&cases[code]);
@ -53,18 +129,24 @@ void add_new_case(uint64_t code, uint64_t mask) {
uint32_t fast_cal(uint64_t code) { uint32_t fast_cal(uint64_t code) {
auto f = FastCal();
f.run();
auto g = GlobalCal();
g.run();
return 0;
auto core = Core(add_new_case); auto core = Core(add_new_case);
cases.empty(); cases.empty();
cache.empty(); cache.empty();
// stop_flag = false;
cases[code] = fast_cal_t { cases[code] = fast_cal_t {
.code = code, .code = code,
.mask = 0, .mask = 0,
// .step = 0,
.last = nullptr, .last = nullptr,
}; };
cache.emplace(&cases[code]); cache.emplace(&cases[code]);
@ -78,11 +160,7 @@ uint32_t fast_cal(uint64_t code) {
break; break;
} }
// if (stop_flag) { // core.next_step(cache.front()->code, cache.front()->mask);
// break;
// }
core.next_step(cache.front()->code, cache.front()->mask);
cache.pop(); cache.pop();
} }

Loading…
Cancel
Save