Explorar el Código

minor update on chatGPT - EventDispatcher C++14.cpp

sfxsfl hace 8 meses
padre
commit
05dadf6787

BIN
chatGPT - EventDispatcher C++14/.vs/chatGPT - EventDispatcher C++14/CopilotIndices/17.13.444.19527/CodeChunks.db


BIN
chatGPT - EventDispatcher C++14/.vs/chatGPT - EventDispatcher C++14/CopilotIndices/17.13.444.19527/SemanticSymbols.db


BIN
chatGPT - EventDispatcher C++14/.vs/chatGPT - EventDispatcher C++14/v17/.suo


BIN
chatGPT - EventDispatcher C++14/.vs/chatGPT - EventDispatcher C++14/v17/Browse.VC.db


+ 3 - 3
chatGPT - EventDispatcher C++14/.vs/chatGPT - EventDispatcher C++14/v17/DocumentLayout.backup.json

@@ -22,9 +22,9 @@
               "Title": "chatGPT - EventDispatcher C\u002B\u002B14.cpp",
               "DocumentMoniker": "C:\\Users\\sfreunde\\source\\repos\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
               "RelativeDocumentMoniker": "chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
-              "ToolTip": "C:\\Users\\sfreunde\\source\\repos\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
-              "RelativeToolTip": "chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
-              "ViewState": "AgIAAJwBAAAAAAAAAADwv7cBAAA9AAAAAAAAAA==",
+              "ToolTip": "C:\\Users\\sfreunde\\source\\repos\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp*",
+              "RelativeToolTip": "chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp*",
+              "ViewState": "AgIAAGcAAAAAAAAAAAAwwH4AAAAlAAAAAAAAAA==",
               "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
               "WhenOpened": "2025-05-06T14:11:43.253Z",
               "EditorCaption": ""

+ 3 - 3
chatGPT - EventDispatcher C++14/.vs/chatGPT - EventDispatcher C++14/v17/DocumentLayout.json

@@ -22,9 +22,9 @@
               "Title": "chatGPT - EventDispatcher C\u002B\u002B14.cpp",
               "DocumentMoniker": "C:\\Users\\sfreunde\\source\\repos\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
               "RelativeDocumentMoniker": "chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
-              "ToolTip": "C:\\Users\\sfreunde\\source\\repos\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp*",
-              "RelativeToolTip": "chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp*",
-              "ViewState": "AgIAALABAAAAAAAAAAAAALABAAAAAAAAAAAAAA==",
+              "ToolTip": "C:\\Users\\sfreunde\\source\\repos\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
+              "RelativeToolTip": "chatGPT - EventDispatcher C\u002B\u002B14\\chatGPT - EventDispatcher C\u002B\u002B14.cpp",
+              "ViewState": "AgIAAGcAAAAAAAAAAAAwwH4AAAAlAAAAAAAAAA==",
               "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
               "WhenOpened": "2025-05-06T14:11:43.253Z",
               "EditorCaption": ""

+ 59 - 24
chatGPT - EventDispatcher C++14/chatGPT - EventDispatcher C++14/chatGPT - EventDispatcher C++14.cpp

@@ -124,7 +124,7 @@ public:
                     // Call handlers with a non-void return type
                     auto handler = dynamic_cast<Handler<R, Args...>*>(handlerBase.get());
                     // Add debug information to identify the type mismatch issue in dynamic_cast
-                    if (!handler) {
+                    if (!handler && 0) {
                         std::cerr << "\n------------------------- emit ------------------------" << std::endl; 
                         std::cerr << "Dynamic cast failed. Handler is NULL. Possible type mismatch or invalid handler." << std::endl;
                         std::cerr << "Expected Handler type: " << typeid(Handler<R, Args...>).name() << std::endl;
@@ -201,28 +201,6 @@ private:
         using ArgsTuple = std::tuple<Args...>;
     };
 
-    template<EventType T>
-    struct HandlerTraits;
-
-    // Specialize HandlerTraits for each EventType to define the expected return type.
-    template<>
-    struct HandlerTraits<EventType::Click> {
-        using ReturnType = bool; // Example: Click events return bool.
-        using ArgsTuple = std::tuple<int>; // Example: Click events take an int argument.
-    };
-
-    template<>
-    struct HandlerTraits<EventType::Hover> {
-        using ReturnType = bool; // Example: Hover events return bool.
-        using ArgsTuple = std::tuple<std::string>; // Example: Hover events take a string argument.
-    };
-
-    template<>
-    struct HandlerTraits<EventType::Custom> {
-        using ReturnType = void; // Example: Custom events return void.
-        using ArgsTuple = std::tuple<>; // Example: Custom events take no arguments.
-    };
-
     template<typename R, typename F, typename... Args>
     HandlerID addHandlerImpl(EventType type, F&& f, std::tuple<Args...>*) {
         // Explicitly construct std::function with the correct return type
@@ -246,6 +224,29 @@ private:
     }
 
 #if 0
+
+    template<EventType T>
+    struct HandlerTraits;
+
+    // Specialize HandlerTraits for each EventType to define the expected return type.
+    template<>
+    struct HandlerTraits<EventType::Click> {
+        using ReturnType = bool; // Example: Click events return bool.
+        using ArgsTuple = std::tuple<int>; // Example: Click events take an int argument.
+    };
+
+    template<>
+    struct HandlerTraits<EventType::Hover> {
+        using ReturnType = bool; // Example: Hover events return bool.
+        using ArgsTuple = std::tuple<std::string>; // Example: Hover events take a string argument.
+    };
+
+    template<>
+    struct HandlerTraits<EventType::Custom> {
+        using ReturnType = void; // Example: Custom events return void.
+        using ArgsTuple = std::tuple<>; // Example: Custom events take no arguments.
+    };
+
     // Implementation: avoid constructing tuple-of-references!
        template<typename R, typename F, typename... Args>
        HandlerID addHandlerImpl(EventType type, F&& f, std::tuple<Args...>*) {
@@ -332,6 +333,25 @@ private:
 #endif
 };
 
+struct Button {
+	int onClick(int x) {
+		std::cout << "Button::onClick(" << x << ")\n";
+		return x;
+	}
+	int onClick_1(int x) {
+		std::cout << "Button::onClick_1(" << x << ")\n";
+		return x * 2;
+	}
+	int onClick_2(int x) {
+		std::cout << "Button::onClick_2(" << x << ")\n";
+		return x;
+	}
+	int onClick_3(int x) {
+		std::cout << "Button::onClick_2(" << x << ")\n";
+		return x;
+	}
+};
+
 struct Widget {
     bool onClick(int x) {
         std::cout << "Widget::onClick(" << x << ")\n";
@@ -357,6 +377,11 @@ struct Widget {
         std::cout << "Widget::onHover_3(" << msg << ")\n";
         return msg;
     }
+
+    int onClick_4(int x) {
+        std::cout << "Widget::onHover_4(" << x << ")\n";
+        return x * 2;
+    }
 };
 
 bool globalHandler(int x) {
@@ -375,6 +400,7 @@ bool globalHandler1(int x) {
 int main() {
     EventDispatcher dispatcher;
     Widget w;
+	Button b;
 #if 1
     // Register handlers and keep their IDs for removal
 	std::cout << "Click Handler registered ...(globalHandler, globalHandler1, lambda)" << std::endl;
@@ -440,11 +466,20 @@ int main() {
     for (const char* b : hoverResults) std::cout << b << " ";
     std::cout << "\n";
 
+    std::cout << "------------------------------------------------------------" << std::endl;
+    std::cout << "New Click Handler registered ... (lambda)" << std::endl;
+    std::cout << ">>  dispatcher.addHandler(EventType::Click, [&w,&b](int x) -> int { return b.onClick_1(w.onClick_4(x)); }" << std::endl;
+    dispatcher.addHandler(EventType::Click, [&w,&b](int x) -> int { return b.onClick_1(w.onClick_4(x)); });
+    auto clickResultsInt = dispatcher.emit<int>(EventType::Click, 7);
+    std::cout << "-----------------" << std::endl;
+    std::cout << "Click results: "; 
+    for (int b : clickResultsInt) std::cout << b << " ";
+    std::cout << "\n";
+
     // Remove a handler and emit again
     std::cout << "------------------------------------------------------------" << std::endl;
     std::cout << "------------------------------------------------------------" << std::endl;
     std::cout << "Remove Handler Click ID 1 ...\n" << std::endl;
-
     dispatcher.removeHandler(EventType::Click, id1_1);
     clickResults = dispatcher.emit<bool>(EventType::Click, 7);
     std::cout << "Click results after removal: ";

BIN
chatGPT - EventDispatcher C++14/chatGPT - EventDispatcher C++14/x64/Debug/chatGPT - EventDispatcher C++14.ilk


BIN
chatGPT - EventDispatcher C++14/chatGPT - EventDispatcher C++14/x64/Debug/chatGPT - EventDispatcher C++14.obj


BIN
chatGPT - EventDispatcher C++14/chatGPT - EventDispatcher C++14/x64/Debug/vc143.idb


BIN
chatGPT - EventDispatcher C++14/chatGPT - EventDispatcher C++14/x64/Debug/vc143.pdb


BIN
chatGPT - EventDispatcher C++14/x64/Debug/chatGPT - EventDispatcher C++14.exe


BIN
chatGPT - EventDispatcher C++14/x64/Debug/chatGPT - EventDispatcher C++14.pdb