fork download
  1. #include <functional>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <algorithm>
  5. #include <utility>
  6. #include <iostream>
  7. #include <cstddef>
  8.  
  9. #define WINAPI
  10. typedef size_t WPARAM;
  11. typedef ptrdiff_t LPARAM;
  12. typedef void * HANDLE;
  13. typedef HANDLE HWND;
  14. typedef ptrdiff_t LRESULT;
  15. typedef uint32_t UINT;
  16.  
  17. LRESULT WINAPI DefWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
  18. return 0;
  19. }
  20.  
  21. class Win32MessageHandler {
  22. public:
  23. typedef UINT HandlerId;
  24.  
  25. typedef std::function<LRESULT (HWND, UINT, WPARAM, LPARAM, bool & handled)> Callback;
  26. static HandlerId RegisterHandler(UINT messageId, Callback const & callback);
  27. static HandlerId RegisterHandler(UINT messageId, Callback && callback);
  28. static void UnregisterHandler(HandlerId const & id);
  29.  
  30. static LRESULT WINAPI WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
  31. private:
  32. static std::unordered_map<uint32_t, Callback> s_messages;
  33. };
  34.  
  35. std::unordered_map<uint32_t, Win32MessageHandler::Callback> Win32MessageHandler::s_messages;
  36.  
  37. Win32MessageHandler::HandlerId Win32MessageHandler::RegisterHandler(UINT messageId, Callback const & callback) {
  38. s_messages.insert(std::make_pair(messageId, callback));
  39. return messageId;
  40. }
  41.  
  42. Win32MessageHandler::HandlerId Win32MessageHandler::RegisterHandler(UINT messageId, Callback && callback) {
  43. s_messages.insert(std::make_pair(messageId, std::move(callback)));
  44. return messageId;
  45. }
  46.  
  47. void Win32MessageHandler::UnregisterHandler(HandlerId const & id) {
  48. s_messages.erase(id);
  49. }
  50.  
  51. LRESULT WINAPI Win32MessageHandler::WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
  52. auto itor = s_messages.find(msg);
  53. if(itor != s_messages.end()) {
  54. bool handled = true;
  55. auto result = itor->second(hwnd, msg, wparam, lparam, handled);
  56. if(handled)
  57. return result;
  58. }
  59.  
  60. return DefWindowProc(hwnd, msg, wparam, lparam);
  61. }
  62.  
  63. class SomeClass {
  64. public:
  65. LRESULT SomeInstanceClassMessageHandlerFunction(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, bool & result) {
  66. std::cout<<"Crazy ass instance function!"<<std::endl;
  67. return 0;
  68. }
  69. };
  70.  
  71. int main(int argc, char ** argv) {
  72. using namespace std::placeholders;
  73.  
  74. auto handlerId = Win32MessageHandler::RegisterHandler(0, [](HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, bool & handled) {
  75. std::cout<<"Hello world"<<std::endl;
  76. return 0;
  77. });
  78.  
  79. SomeClass s;
  80.  
  81. Win32MessageHandler::RegisterHandler(1,
  82. Win32MessageHandler::Callback(
  83. std::bind(
  84. &SomeClass::SomeInstanceClassMessageHandlerFunction,
  85. &s,
  86. _1,
  87. _2,
  88. _3,
  89. _4,
  90. _5
  91. )
  92. )
  93. );
  94. Win32MessageHandler::WindowProc(0, 0, 0, 0);
  95. Win32MessageHandler::WindowProc(0, 1, 0, 0);
  96.  
  97. Win32MessageHandler::UnregisterHandler(handlerId);
  98.  
  99. Win32MessageHandler::WindowProc(0, 0, 0, 0);
  100. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Hello world
Crazy ass instance function!