cpp-common: Add hash() and isSame() methods to BorrowedObj
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 22 Feb 2022 20:43:46 +0000 (15:43 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
This will allow us to use the C++ wrapper objects in
`std::unordered_map` by using these methods in custom hasher and isEqual
objects.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I4e018ece9ea4ca755f6612cd9adec408667b202d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7356
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10755
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2/internal/borrowed-obj.hpp

index 4f3645987c00f1d67b02611b42959a4144161e25..b064a6b25f854b55e79636345764574647909d49 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP
 #define BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP
 
+#include <functional>
 #include <type_traits>
 
 #include "common/assert.h"
@@ -108,6 +109,26 @@ protected:
         return *this;
     }
 
+public:
+    /*
+     * Returns a hash of this object, solely based on its raw libbabeltrace2
+     * pointer.
+     */
+    std::size_t hash() const noexcept
+    {
+        return std::hash<_LibObjPtr> {}(_mLibObjPtr);
+    }
+
+    /*
+     * Returns whether or not this object is the exact same as `other`,
+     * solely based on the raw libbabeltrace2 pointers.
+     */
+    bool isSame(const _ThisBorrowedObj& other) const noexcept
+    {
+        return _mLibObjPtr == other._mLibObjPtr;
+    }
+
+protected:
     /* Wrapped libbabeltrace2 object pointer */
     _LibObjPtr _libObjPtr() const noexcept
     {
This page took 0.024884 seconds and 4 git commands to generate.