| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /* printf example */
- #include <stdio.h>
- #include <cstdio>
- #include <iostream>
- #include <functional> // std::function
- #include <stdint.h> // uint64_t
- #include <unordered_map> // std::unordered_map
- #include <utility> // std::move
- #include <vector> // 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<typename R, typename P1, typename P2>
- 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<typename R, class T, typename P1, typename P2, R(T::* Func)(P1, P2)>
- R Wrapper(void* o, P1 a1, P2 a2)
- {
- return (static_cast<T*>(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<float, int, int> callback)
- {
- return callback(n1, n2);
- }
- int main()
- {
- Foo f;
- Callback<float, int, int> cb
- (&Wrapper<float, Foo, int, int, &Foo::Average>, &f);
- float result = Calculate(50, 100, cb);
- // result == 75.0f
- return 0;
- }
- #endif
- #if 0
- namespace my
- {
- using Callback = function<void()>;
- template< class Key, class Value > using Map_ = unordered_map<Key, Value>;
- class Subject
- {
- public:
- enum Id : uint64_t {};
- private:
- Map_<uint64_t, Callback> 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<void(int, const std::string&)> 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
|