/* printf example */ #include #include #include #include // std::function #include // uint64_t #include // std::unordered_map #include // std::move #include // std::vector using namespace std; #if 1 // // https://manderc.com/types/functionpointertype/index.php // void globalfunction(void * pthis, int i) { printf("Global globalfunction (%i)\n", i); } static void staticglobalfunction(void * pthis,int i) { printf("Static Global (%i)\n", i); } namespace Name_Space { void namespacefunc(void * pthis, int i) { printf("Namespace (%i)\n", i); } } #if 0 void (*returnfunction(const char* str))(int) { printf("Return \"%s\":\n", str); return &globalfunction; } #endif class ExternClass { public: void externClassfunction(void * pthis, int i) { printf("externClassfunction (%i)\n", i); } static void staticExternClassfunction(void * pthis, int i) { printf("staticExternClassfunction (%i)\n", i); } }; class Test { private: void (*funcptr1)(void*, int i) = &globalfunction; unsigned int myTestVar = 10; public: void (*funcptr2)(void* pthis, int i) = nullptr; void (*funcptr3)(void* pthis, int i) = nullptr; void (*funcptr4)(void* pthis, int i) = nullptr; void (Test::* funcptr5)(int i); void classfunction(int i) { printf("Test Class local classfunction (%i)\n", i); } void callglobal(int i) { funcptr1 = &globalfunction; } void callclass(int i) { funcptr5 = &Test::classfunction; } void run(int i) { (*funcptr1)(this, i); (*funcptr2)(this, i); (*funcptr3)(this, i); (*funcptr4)(this, i); // (this->*funcptr5)(this, i); } }; int main() { Test test; ExternClass extClass; // Global function with class-function-ptr outside class test.funcptr2 = &globalfunction; // Function in namespace with local function pointer test.funcptr3 = &Name_Space::namespacefunc; // Class function with class-function-ptr outside class test.funcptr4 = &ExternClass::staticExternClassfunction; // Class function with class-function-ptr outside class test.funcptr5 = &Test::classfunction; // Class function with local function-ptr void (Test:: * classfunc)(int i) = &Test::classfunction; test.run(10); // (*returnfunction("global"))(10); // printf("Using typedef:\n"); #if 0 typedef void (*functiontype)(int); functiontype typefuncptr = &globalfunction; (*typefuncptr)(11); #endif return 0; } #endif #if 0 template class Callback { public: typedef R(*FuncType)(void*, P1, P2); Callback() : func(0), obj(0) {} Callback(FuncType f, void* o) : func(f), obj(o) {} R operator()(P1 a1, P2 a2) { return (*func)(obj, a1, a2); } private: FuncType func; void* obj; }; template R Wrapper(void* o, P1 a1, P2 a2) { return (static_cast(o)->*Func)(a1, a2); } class Foo { public: float Average(int n1, int n2) { return (n1 + n2) / 2.0f; } }; float Calculate(int n1, int n2, Callback callback) { return callback(n1, n2); } int main() { Foo f; Callback cb (&Wrapper, &f); float result = Calculate(50, 100, cb); // result == 75.0f return 0; } #endif #if 0 namespace my { using Callback = function; template< class Key, class Value > using Map_ = unordered_map; class Subject { public: enum Id : uint64_t {}; private: Map_ m_callbacks; static auto id_value() -> uint64_t& { static uint64_t the_id; return the_id; } public: auto add_listener(Callback cb) -> Id { const auto id = Id(++id_value()); m_callbacks.emplace(id, move(cb)); return id; } auto remove_listener(const Id id) -> bool { const auto it = m_callbacks.find(id); if (it == m_callbacks.end()) { return false; } m_callbacks.erase(it); return true; } void notify_all() const { for (const auto& pair : m_callbacks) { pair.second(); } } }; } struct Observer_1 { void notify() { cout << "Observer_1::notify() called." << endl; } }; struct Observer_2 { void notify() { cout << "Observer_2::notify() called." << endl; } }; auto main() -> int { my::Subject subject; Observer_1 one; Observer_2 two; using Id = my::Subject::Id; const Id listener_id_1 = subject.add_listener([&] { one.notify(); }); const Id listener_id_2 = subject.add_listener([&] { two.notify(); }); cout << "After adding two listeners:" << endl; subject.notify_all(); cout << endl; subject.remove_listener(listener_id_1) and (cout << "Removed listener 1." << endl) or (cout << "Did not find registration of listener 1." << endl); cout << endl; cout << "After removing or attempting to remove listener 1:" << endl; subject.notify_all(); } #endif #if 0 class A { public: std::function m_CbFunc = nullptr; void foo() { if (m_CbFunc) { m_CbFunc(100, "event fired"); } } }; class B { public: unsigned int TestVariable = 10; B() { auto aFunc = std::bind(&B::eventHandler, this, std::placeholders::_1, std::placeholders::_2); anObjA.m_CbFunc = aFunc; } void eventHandler(int i, const std::string& s) { std::cout << s << ": " << i << std::endl; } void DoSomethingOnA() { anObjA.foo(); } A anObjA; }; int main(int argc, char* argv[]) { B anObjB; anObjB.DoSomethingOnA(); } #endif