From da46eb9e7e0f6e069e7fc06176849d213876bfe3 Mon Sep 17 00:00:00 2001
From: Nat Goodspeed <nat@lindenlab.com>
Date: Wed, 24 Jun 2009 01:54:57 +0000
Subject: [PATCH] DEV-31980: Extend LLEventDispatcher to handle const as well
 as non-const methods. Introduce LLAppViewerListener based on
 LLDispatchListener, instantiate a static one in llappviewer.cpp. Initial
 implementation only supports ["op"] == "requestQuit".

---
 indra/llcommon/lleventdispatcher.h    | 131 ++++++++++++++++++++++----
 indra/newview/CMakeLists.txt          |   2 +
 indra/newview/llappviewer.cpp         |   3 +
 indra/newview/llappviewerlistener.cpp |  33 +++++++
 indra/newview/llappviewerlistener.h   |  34 +++++++
 5 files changed, 187 insertions(+), 16 deletions(-)
 create mode 100644 indra/newview/llappviewerlistener.cpp
 create mode 100644 indra/newview/llappviewerlistener.h

diff --git a/indra/llcommon/lleventdispatcher.h b/indra/llcommon/lleventdispatcher.h
index d75055fd6f2..e43d967ed47 100644
--- a/indra/llcommon/lleventdispatcher.h
+++ b/indra/llcommon/lleventdispatcher.h
@@ -23,6 +23,40 @@
 #include "llevents.h"
 
 class LLSD;
+/*==========================================================================*|
+class LLEventDispatcher;
+
+namespace LLEventDetail
+{
+    /// For a given call to add(), decide whether we're being passed an
+    /// unbound member function pointer or a plain callable.
+    /// Default case.
+    template <typename CALLABLE>
+    struct AddCallable
+    {
+        void operator()(LLEventDispatcher& disp, const std::string& name,
+                        const CALLABLE& callable, const LLSD& required);
+    };
+
+    /// Unbound member function pointer
+    template <class CLASS>
+    struct AddCallable<void (CLASS::*)(const LLSD&)>
+    {
+        typedef void (CLASS::*Method)(const LLSD&);
+        void operator()(LLEventDispatcher& disp, const std::string& name,
+                        Method method, const LLSD& required);
+    };
+
+    /// Unbound const member function pointer
+    template <class CLASS>
+    struct AddCallable<void (CLASS::*)(const LLSD&) const>
+    {
+        typedef void (CLASS::*Method)(const LLSD&) const;
+        void operator()(LLEventDispatcher& disp, const std::string& name,
+                        Method method, const LLSD& required);
+    };
+}
+|*==========================================================================*/
 
 /**
  * Given an LLSD map, examine a string-valued key and call a corresponding
@@ -39,28 +73,36 @@ class LLEventDispatcher
     /// Accept any C++ callable, typically a boost::bind() expression
     typedef boost::function<void(const LLSD&)> Callable;
 
-    /// Register a @a callable by @a name. The optional @a required parameter
-    /// is used to validate the structure of each incoming event (see
-    /// llsd_matches()).
+    /**
+     * Register a @a callable by @a name. The optional @a required parameter
+     * is used to validate the structure of each incoming event (see
+     * llsd_matches()).
+     */
     void add(const std::string& name, const Callable& callable, const LLSD& required=LLSD());
+/*==========================================================================*|
+    {
+        LLEventDetail::AddCallable<CALLABLE>()(*this, name, callable, required);
+    }
+|*==========================================================================*/
 
-    /// Special case: a subclass of this class can register a @a method
-    /// without explicitly specifying the <tt>boost::bind()</tt> expression.
-    /// The optional @a required parameter is used to validate the structure
-    /// of each incoming event (see llsd_matches()).
+    /**
+     * Special case: a subclass of this class can pass an unbound member
+     * function pointer without explicitly specifying the
+     * <tt>boost::bind()</tt> expression.
+     */
     template <class CLASS>
     void add(const std::string& name, void (CLASS::*method)(const LLSD&),
              const LLSD& required=LLSD())
     {
-        CLASS* downcast = dynamic_cast<CLASS*>(this);
-        if (! downcast)
-        {
-            addFail(name, typeid(CLASS).name());
-        }
-        else
-        {
-            add(name, boost::bind(method, downcast, _1), required);
-        }
+        addMethod<CLASS>(name, method, required);
+    }
+
+    /// Overload for both const and non-const methods
+    template <class CLASS>
+    void add(const std::string& name, void (CLASS::*method)(const LLSD&) const,
+             const LLSD& required=LLSD())
+    {
+        addMethod<CLASS>(name, method, required);
     }
 
     /// Unregister a callable
@@ -78,6 +120,19 @@ class LLEventDispatcher
     void operator()(const LLSD& event) const;
 
 private:
+    template <class CLASS, typename METHOD>
+    void addMethod(const std::string& name, const METHOD& method, const LLSD& required)
+    {
+        CLASS* downcast = dynamic_cast<CLASS*>(this);
+        if (! downcast)
+        {
+            addFail(name, typeid(CLASS).name());
+        }
+        else
+        {
+            add(name, boost::bind(method, downcast, _1), required);
+        }
+    }
     void addFail(const std::string& name, const std::string& classname) const;
     /// try to dispatch, return @c true if success
     bool attemptCall(const std::string& name, const LLSD& event) const;
@@ -87,6 +142,50 @@ class LLEventDispatcher
     DispatchMap mDispatch;
 };
 
+/*==========================================================================*|
+/// Have to implement these template specialization methods after
+/// LLEventDispatcher so they can use its methods
+template <typename CALLABLE>
+void LLEventDetail::AddCallable<CALLABLE>::operator()(
+    LLEventDispatcher& disp, const std::string& name, const CALLABLE& callable, const LLSD& required)
+{
+    disp.addImpl(name, callable, required);
+}
+
+template <class CLASS>
+void LLEventDetail::AddCallable<void (CLASS::*)(const LLSD&)>::operator()(
+    LLEventDispatcher& disp, const std::string& name, const Method& method, const LLSD& required)
+{
+    CLASS* downcast = dynamic_cast<CLASS*>(&disp);
+    if (! downcast)
+    {
+        disp.addFail(name, typeid(CLASS).name());
+    }
+    else
+    {
+        disp.addImpl(name, boost::bind(method, downcast, _1), required);
+    }
+}
+
+/// Have to overload for both const and non-const methods
+template <class CLASS>
+void LLEventDetail::AddCallable<void (CLASS::*)(const LLSD&) const>::operator()(
+    LLEventDispatcher& disp, const std::string& name, const Method& method, const LLSD& required)
+{
+    // I am severely bummed that I have, as yet, found no way short of a
+    // macro to avoid replicating the (admittedly brief) body of this overload.
+    CLASS* downcast = dynamic_cast<CLASS*>(&disp);
+    if (! downcast)
+    {
+        disp.addFail(name, typeid(CLASS).name());
+    }
+    else
+    {
+        disp.addImpl(name, boost::bind(method, downcast, _1), required);
+    }
+}
+|*==========================================================================*/
+
 /**
  * Bundle an LLEventPump and a listener with an LLEventDispatcher. A class
  * that contains (or derives from) LLDispatchListener need only specify the
diff --git a/indra/newview/CMakeLists.txt b/indra/newview/CMakeLists.txt
index 5d79dfbc3e9..29031418b8a 100644
--- a/indra/newview/CMakeLists.txt
+++ b/indra/newview/CMakeLists.txt
@@ -73,6 +73,7 @@ set(viewer_SOURCE_FILES
     llagentpilot.cpp
     llanimstatelabels.cpp
     llappviewer.cpp
+    llappviewerlistener.cpp
     llassetuploadresponders.cpp
     llassetuploadqueue.cpp
     llaudiosourcevo.cpp
@@ -468,6 +469,7 @@ set(viewer_HEADER_FILES
     llanimstatelabels.h
     llappearance.h
     llappviewer.h
+    llappviewerlistener.h
     llassetuploadresponders.h
     llassetuploadqueue.h
     llaudiosourcevo.h
diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp
index 455e987da0b..a3b41b4ace9 100644
--- a/indra/newview/llappviewer.cpp
+++ b/indra/newview/llappviewer.cpp
@@ -177,7 +177,10 @@
 //----------------------------------------------------------------------------
 // llviewernetwork.h
 #include "llviewernetwork.h"
+// define a self-registering event API object
+#include "llappviewerlistener.h"
 
+static LLAppViewerListener sAppViewerListener("LLAppViewer", LLAppViewer::instance());
 
 ////// Windows-specific includes to the bottom - nasty defines in these pollute the preprocessor
 //
diff --git a/indra/newview/llappviewerlistener.cpp b/indra/newview/llappviewerlistener.cpp
new file mode 100644
index 00000000000..a8c98b17a73
--- /dev/null
+++ b/indra/newview/llappviewerlistener.cpp
@@ -0,0 +1,33 @@
+/**
+ * @file   llappviewerlistener.cpp
+ * @author Nat Goodspeed
+ * @date   2009-06-23
+ * @brief  Implementation for llappviewerlistener.
+ * 
+ * $LicenseInfo:firstyear=2009&license=viewergpl$
+ * Copyright (c) 2009, Linden Research, Inc.
+ * $/LicenseInfo$
+ */
+
+// Precompiled header
+#include "llviewerprecompiledheaders.h"
+// associated header
+#include "llappviewerlistener.h"
+// STL headers
+// std headers
+// external library headers
+// other Linden headers
+#include "llappviewer.h"
+
+LLAppViewerListener::LLAppViewerListener(const std::string& pumpname, LLAppViewer* llappviewer):
+    LLDispatchListener(pumpname, "op"),
+    mAppViewer(llappviewer)
+{
+    // add() every method we want to be able to invoke via this event API.
+    add("requestQuit", &LLAppViewerListener::requestQuit);
+}
+
+void LLAppViewerListener::requestQuit(const LLSD& event) const
+{
+    mAppViewer->requestQuit();
+}
diff --git a/indra/newview/llappviewerlistener.h b/indra/newview/llappviewerlistener.h
new file mode 100644
index 00000000000..ab17dd1d904
--- /dev/null
+++ b/indra/newview/llappviewerlistener.h
@@ -0,0 +1,34 @@
+/**
+ * @file   llappviewerlistener.h
+ * @author Nat Goodspeed
+ * @date   2009-06-18
+ * @brief  Wrap subset of LLAppViewer API in event API
+ * 
+ * $LicenseInfo:firstyear=2009&license=viewergpl$
+ * Copyright (c) 2009, Linden Research, Inc.
+ * $/LicenseInfo$
+ */
+
+#if ! defined(LL_LLAPPVIEWERLISTENER_H)
+#define LL_LLAPPVIEWERLISTENER_H
+
+#include "lleventdispatcher.h"
+
+class LLAppViewer;
+class LLSD;
+
+/// Listen on an LLEventPump with specified name for LLAppViewer request events.
+class LLAppViewerListener: public LLDispatchListener
+{
+public:
+    /// Specify the pump name on which to listen, and bind the LLAppViewer
+    /// instance to use (e.g. LLAppViewer::instance()).
+    LLAppViewerListener(const std::string& pumpname, LLAppViewer* llappviewer);
+
+private:
+    void requestQuit(const LLSD& event) const;
+
+    LLAppViewer* mAppViewer;
+};
+
+#endif /* ! defined(LL_LLAPPVIEWERLISTENER_H) */
-- 
GitLab