From ae3081b232359c7e763d6c3725eb79b6999b2551 Mon Sep 17 00:00:00 2001
From: Justin Carpentier <justin.carpentier@inria.fr>
Date: Mon, 10 Jun 2024 12:16:05 +0200
Subject: [PATCH] core: add id helper

---
 CMakeLists.txt          |  1 +
 include/eigenpy/fwd.hpp |  1 +
 include/eigenpy/id.hpp  | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 include/eigenpy/id.hpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index bcc8f989..94bf5d85 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -232,6 +232,7 @@ set(${PROJECT_NAME}_HEADERS
     include/eigenpy/eigen-to-python.hpp
     include/eigenpy/eigen-from-python.hpp
     include/eigenpy/eigen-typedef.hpp
+    include/eigenpy/id.hpp
     include/eigenpy/numpy-map.hpp
     include/eigenpy/geometry.hpp
     include/eigenpy/geometry-conversion.hpp
diff --git a/include/eigenpy/fwd.hpp b/include/eigenpy/fwd.hpp
index c511b569..acc8edc1 100644
--- a/include/eigenpy/fwd.hpp
+++ b/include/eigenpy/fwd.hpp
@@ -200,5 +200,6 @@ struct has_operator_equal : internal::has_operator_equal_impl<T1, T2>::type {};
 }  // namespace eigenpy
 
 #include "eigenpy/alignment.hpp"
+#include "eigenpy/id.hpp"
 
 #endif  // ifndef __eigenpy_fwd_hpp__
diff --git a/include/eigenpy/id.hpp b/include/eigenpy/id.hpp
new file mode 100644
index 00000000..af56d79f
--- /dev/null
+++ b/include/eigenpy/id.hpp
@@ -0,0 +1,33 @@
+//
+// Copyright (c) 2024 INRIA
+//
+
+#ifndef __eigenpy_id_hpp__
+#define __eigenpy_id_hpp__
+
+#include <boost/python.hpp>
+#include <boost/cstdint.hpp>
+
+namespace eigenpy {
+
+///
+/// \brief Add the Python method id to retrieving a unique id for a given object
+/// exposed with Boost.Python
+///
+template <class C>
+struct IdVisitor : public bp::def_visitor<IdVisitor<C> > {
+  template <class PyClass>
+  void visit(PyClass& cl) const {
+    cl.def("id", &id, bp::arg("self"),
+           "Returns the unique identity of an object.\n"
+           "For object held in C++, it corresponds to its memory address.");
+  }
+
+ private:
+  static boost::int64_t id(const C& self) {
+    return boost::int64_t(reinterpret_cast<const void*>(&self));
+  }
+};
+}  // namespace eigenpy
+
+#endif  // ifndef __eigenpy_id_hpp__
-- 
GitLab