summaryrefslogtreecommitdiff
path: root/indra/llcommon
diff options
context:
space:
mode:
authorNat Goodspeed <nat@lindenlab.com>2024-08-30 17:13:26 -0400
committerNat Goodspeed <nat@lindenlab.com>2024-08-30 17:13:26 -0400
commit2eb0d173c3d24997a70b5ca9e78562ba48fc2f51 (patch)
treeefc13fe11e0f2c5e0ca4cce83a157f9c8929b6d5 /indra/llcommon
parenta662fea2840bd6e8b7856b5f3c8e2f43e706178e (diff)
Add LLIntTracker<T>, an LLInstanceTracker<T, int> with generated keys.
The point of LLIntTracker is to generate its keys implicitly, so that its int getKey() can be treated more or less like an instance pointer, with the added bonus that the key can be passed around via LLSD. LLIntTracker generates random int keys to try to make it a little harder for one script to mess with an LLIntTracker instance belonging to another.
Diffstat (limited to 'indra/llcommon')
-rw-r--r--indra/llcommon/CMakeLists.txt1
-rw-r--r--indra/llcommon/llinttracker.h43
2 files changed, 44 insertions, 0 deletions
diff --git a/indra/llcommon/CMakeLists.txt b/indra/llcommon/CMakeLists.txt
index 8472eac9f6..8577d94be1 100644
--- a/indra/llcommon/CMakeLists.txt
+++ b/indra/llcommon/CMakeLists.txt
@@ -189,6 +189,7 @@ set(llcommon_HEADER_FILES
llinitparam.h
llinstancetracker.h
llinstancetrackersubclass.h
+ llinttracker.h
llkeybind.h
llkeythrottle.h
llleap.h
diff --git a/indra/llcommon/llinttracker.h b/indra/llcommon/llinttracker.h
new file mode 100644
index 0000000000..86c30bc7aa
--- /dev/null
+++ b/indra/llcommon/llinttracker.h
@@ -0,0 +1,43 @@
+/**
+ * @file llinttracker.h
+ * @author Nat Goodspeed
+ * @date 2024-08-30
+ * @brief LLIntTracker isa LLInstanceTracker with generated int keys.
+ *
+ * $LicenseInfo:firstyear=2024&license=viewerlgpl$
+ * Copyright (c) 2024, Linden Research, Inc.
+ * $/LicenseInfo$
+ */
+
+#if ! defined(LL_LLINTTRACKER_H)
+#define LL_LLINTTRACKER_H
+
+#include "llinstancetracker.h"
+
+template <typename T>
+class LLIntTracker: public LLInstanceTracker<T, int>
+{
+ using super = LLInstanceTracker<T, int>;
+public:
+ LLIntTracker():
+ super(getUniqueKey())
+ {}
+
+private:
+ static int getUniqueKey()
+ {
+ // Find a random key that does NOT already correspond to an instance.
+ // Passing a duplicate key to LLInstanceTracker would do Bad Things.
+ int key;
+ do
+ {
+ key = std::rand();
+ } while (super::getInstance(key));
+ // This could be racy, if we were instantiating new LLIntTracker<T>s
+ // on multiple threads. If we need that, have to lock between checking
+ // getInstance() and constructing the new super.
+ return key;
+ }
+};
+
+#endif /* ! defined(LL_LLINTTRACKER_H) */