From 90ff656d697b784d07467855476a50b258112d71 Mon Sep 17 00:00:00 2001
From: jcarpent <jcarpent@laas.fr>
Date: Wed, 19 Oct 2016 14:31:56 +0200
Subject: [PATCH] [C++] Add check of registration at runtime

---
 CMakeLists.txt       |  1 +
 src/details.hpp      |  2 ++
 src/exception.cpp    |  7 +++++--
 src/registration.hpp | 45 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 src/registration.hpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4962eb9..8acb0ef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -81,6 +81,7 @@ SET(HEADERS
   src/map.hpp
   src/geometry.hpp
   src/memory.hpp
+  src/registration.hpp
   src/angle-axis.hpp
   src/quaternion.hpp
 )
diff --git a/src/details.hpp b/src/details.hpp
index 8ca96c9..406b133 100644
--- a/src/details.hpp
+++ b/src/details.hpp
@@ -24,6 +24,7 @@
 #include <iostream>
 
 #include "eigenpy/eigenpy.hpp"
+#include "eigenpy/registration.hpp"
 #include "eigenpy/exception.hpp"
 #include "eigenpy/map.hpp"
 
@@ -206,6 +207,7 @@ namespace eigenpy
   template<typename MatType,typename EigenEquivalentType>
   void enableEigenPySpecific()
   {
+    if(check_registration<MatType>()) return;
     numpy_import_array();
     
     boost::python::to_python_converter<MatType,EigenToPy<MatType,MatType> >();
diff --git a/src/exception.cpp b/src/exception.cpp
index b8ac24c..e67b298 100644
--- a/src/exception.cpp
+++ b/src/exception.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 LAAS-CNRS
+ * Copyright (c) 2015-2016 LAAS-CNRS
  *
  * This file is part of eigenpy.
  * eigenpy is free software: you can redistribute it and/or
@@ -14,7 +14,8 @@
  * with eigenpy.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <eigenpy/exception.hpp>
+#include "eigenpy/exception.hpp"
+#include "eigenpy/registration.hpp"
 
 
 namespace eigenpy
@@ -30,6 +31,8 @@ namespace eigenpy
 
   void Exception::registerException()
   {
+    if(check_registration<eigenpy::Exception>()) return;
+    
     pyType = boost::python::class_<eigenpy::Exception>
       ("Exception",boost::python::init<std::string>())
       .add_property("message", &eigenpy::Exception::copyMessage)
diff --git a/src/registration.hpp b/src/registration.hpp
new file mode 100644
index 0000000..482335f
--- /dev/null
+++ b/src/registration.hpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2016, Justin Carpentier, LAAS-CNRS
+ *
+ * This file is part of eigenpy.
+ * eigenpy is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ * eigenpy is distributed in the hope that it will be
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.  You should
+ * have received a copy of the GNU Lesser General Public License along
+ * with eigenpy.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __eigenpy_registration_hpp__
+#define __eigenpy_registration_hpp__
+
+#include <boost/python.hpp>
+
+namespace eigenpy
+{
+  ///
+  /// \brief Check at runtime the registration of the type T inside the boost python registry.
+  ///
+  /// \tparam T The type to check the registration.
+  ///
+  /// \returns true if the type T is already registered.
+  ///
+  template<typename T>
+  inline bool check_registration()
+  {
+    namespace bp = boost::python;
+    
+    const bp::type_info info = bp::type_id<T>();
+    const bp::converter::registration* reg = bp::converter::registry::query(info);
+    if (reg == NULL) return false;
+    else if ((*reg).m_to_python == NULL) return false;
+    
+    return true;
+  }
+}
+
+#endif // ifndef __eigenpy_registration_hpp__
-- 
GitLab