From 1eedae96bdc3a31c3584ebba49b7655ff80e3263 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 12 Jan 2023 12:07:33 +0800 Subject: [PATCH] update: class member function pointer demo --- src/fast_cal.cc | 80 +++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/src/fast_cal.cc b/src/fast_cal.cc index 790b5fe..d12310b 100644 --- a/src/fast_cal.cc +++ b/src/fast_cal.cc @@ -17,9 +17,6 @@ std::queue cache; std::unordered_map cases; -void add_global_demo(uint64_t code) { - std::cout << RawCode(code).dump_case() << std::endl; -} class FastCal; class GlobalCal; @@ -27,78 +24,63 @@ class GlobalCal; template class CoreDemo { public: - int code{0}; - typedef void (T::*release_t)(uint64_t); -// 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; + void next(uint64_t code, T *f, release_t release_func) { + std::cout << "Core get code = " << code << std::endl; + std::cout << "Core callback first time" << std::endl; + (f->*release_func)(++code); + std::cout << "Core callback second time" << std::endl; + (f->*release_func)(++code); + std::cout << "Core function exit" << std::endl; } }; - class FastCal { public: - int dat; + uint64_t data; - void add(uint64_t code) { - std::cout << RawCode(code).dump_case() << std::endl; - ++dat; + explicit FastCal(uint64_t dat) : data(dat) {} + + void callback(uint64_t code) { + std::cout << "FastCal get callback " << code << std::endl; + data = code; + std::cout << "FastCal set data " << data << std::endl; } void run() { auto cd = CoreDemo(); -// cd.next(add_global_demo); -// cd.next(this->*add); - - cd.next(this, &FastCal::add); + std::cout << "FastCal data = " << data << std::endl; - ++dat; + cd.next(data, this, &FastCal::callback); } }; - class GlobalCal { public: - int dat; + uint64_t data; - void add(uint64_t code) { - std::cout << RawCode(code).dump_case() << std::endl; - ++dat; + explicit GlobalCal(uint64_t dat) : data(dat) {} + + void callback(uint64_t code) { + std::cout << "GlobalCal get callback " << code << std::endl; + data = code; + std::cout << "GlobalCal set data " << data << std::endl; } void run() { auto cd = CoreDemo(); - cd.next(this, &GlobalCal::add); + std::cout << "GlobalCal data = " << data << std::endl; - ++dat; + cd.next(data, this, &GlobalCal::callback); } }; -//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) { @@ -129,12 +111,18 @@ void add_new_case(uint64_t code, uint64_t mask) { uint32_t fast_cal(uint64_t code) { - auto f = FastCal(); - f.run(); + auto f1 = FastCal(1000); + auto f2 = FastCal(2000); + auto g = GlobalCal(3000); - auto g = GlobalCal(); + f2.run(); + f1.run(); + f1.run(); g.run(); +// auto g = GlobalCal(); +// g.run(); + return 0;