From cc3d21cceac7d6c1b2fc9297330fa819855f6e5b Mon Sep 17 00:00:00 2001
From: Nat Goodspeed <nat@lindenlab.com>
Date: Tue, 20 Dec 2022 09:53:58 -0500
Subject: [PATCH] DRTVWR-558: Tweak LLEventDispatcher.

Instead of std::map<std::string, boost::shared_ptr>, use std::unique_ptr as
the mapped_type, using emplace() to store new entries. This more correctly
captures the desired semantics: we have no intention of passing around the
pointers in the map, we just want the map to delete them on destruction.

Use std::function instead of boost::function.

(cherry picked from commit 7ba53ef82db5683756e296225f0c8b838420a26e)
---
 indra/llcommon/lleventdispatcher.cpp | 12 +++---------
 indra/llcommon/lleventdispatcher.h   | 21 +++++++--------------
 2 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/indra/llcommon/lleventdispatcher.cpp b/indra/llcommon/lleventdispatcher.cpp
index 0c3bb35cfe5..3e456014294 100644
--- a/indra/llcommon/lleventdispatcher.cpp
+++ b/indra/llcommon/lleventdispatcher.cpp
@@ -561,9 +561,7 @@ void LLEventDispatcher::addArrayParamsDispatchEntry(const std::string& name,
                                                     const invoker_function& invoker,
                                                     LLSD::Integer arity)
 {
-    mDispatch.insert(
-        DispatchMap::value_type(name, DispatchMap::mapped_type(
-                                    new ArrayParamsDispatchEntry(desc, invoker, arity))));
+    mDispatch.emplace(name, new ArrayParamsDispatchEntry(desc, invoker, arity));
 }
 
 void LLEventDispatcher::addMapParamsDispatchEntry(const std::string& name,
@@ -572,18 +570,14 @@ void LLEventDispatcher::addMapParamsDispatchEntry(const std::string& name,
                                                   const LLSD& params,
                                                   const LLSD& defaults)
 {
-    mDispatch.insert(
-        DispatchMap::value_type(name, DispatchMap::mapped_type(
-                                    new MapParamsDispatchEntry(name, desc, invoker, params, defaults))));
+    mDispatch.emplace(name, new MapParamsDispatchEntry(name, desc, invoker, params, defaults));
 }
 
 /// Register a callable by name
 void LLEventDispatcher::add(const std::string& name, const std::string& desc,
                             const Callable& callable, const LLSD& required)
 {
-    mDispatch.insert(
-        DispatchMap::value_type(name, DispatchMap::mapped_type(
-                                    new LLSDDispatchEntry(desc, callable, required))));
+    mDispatch.emplace(name, new LLSDDispatchEntry(desc, callable, required));
 }
 
 /// Unregister a callable
diff --git a/indra/llcommon/lleventdispatcher.h b/indra/llcommon/lleventdispatcher.h
index 6d1df86fead..cf88dced12b 100644
--- a/indra/llcommon/lleventdispatcher.h
+++ b/indra/llcommon/lleventdispatcher.h
@@ -56,9 +56,6 @@ static const auto nil_(nil);
 static const auto& nil(nil_);
 #endif
 
-#include <string>
-#include <boost/shared_ptr.hpp>
-#include <boost/function.hpp>
 #include <boost/bind.hpp>
 #include <boost/iterator/transform_iterator.hpp>
 #include <boost/function_types/is_nonmember_callable_builtin.hpp>
@@ -73,6 +70,9 @@ static const auto& nil(nil_);
 #include <boost/mpl/end.hpp>
 #include <boost/mpl/next.hpp>
 #include <boost/mpl/deref.hpp>
+#include <functional>               // std::function
+#include <memory>                   // std::unique_ptr
+#include <string>
 #include <typeinfo>
 #include "llevents.h"
 #include "llsdutil.h"
@@ -96,7 +96,7 @@ class LL_COMMON_API LLEventDispatcher
 
     /// Accept any C++ callable with the right signature, typically a
     /// boost::bind() expression
-    typedef boost::function<void(const LLSD&)> Callable;
+    typedef std::function<void(const LLSD&)> Callable;
 
     /**
      * Register a @a callable by @a name. The passed @a callable accepts a
@@ -293,14 +293,7 @@ class LL_COMMON_API LLEventDispatcher
         virtual void call(const std::string& desc, const LLSD& event) const = 0;
         virtual LLSD addMetadata(LLSD) const = 0;
     };
-    // Tried using boost::ptr_map<std::string, DispatchEntry>, but ptr_map<>
-    // wants its value type to be "clonable," even just to dereference an
-    // iterator. I don't want to clone entries -- if I have to copy an entry
-    // around, I want it to continue pointing to the same DispatchEntry
-    // subclass object. However, I definitely want DispatchMap to destroy
-    // DispatchEntry if no references are outstanding at the time an entry is
-    // removed. This looks like a job for boost::shared_ptr.
-    typedef std::map<std::string, boost::shared_ptr<DispatchEntry> > DispatchMap;
+    typedef std::map<std::string, std::unique_ptr<DispatchEntry> > DispatchMap;
 
 public:
     /// We want the flexibility to redefine what data we store per name,
@@ -363,10 +356,10 @@ class LL_COMMON_API LLEventDispatcher
     struct invoker;
 
     // deliver LLSD arguments one at a time
-    typedef boost::function<LLSD()> args_source;
+    typedef std::function<LLSD()> args_source;
     // obtain args from an args_source to build param list and call target
     // function
-    typedef boost::function<void(const args_source&)> invoker_function;
+    typedef std::function<void(const args_source&)> invoker_function;
 
     template <typename Function>
     invoker_function make_invoker(Function f);
-- 
GitLab