Skip to content

Commit 90ff656

Browse files
committed
[C++] Add check of registration at runtime
1 parent 6a502cd commit 90ff656

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ SET(HEADERS
8181
src/map.hpp
8282
src/geometry.hpp
8383
src/memory.hpp
84+
src/registration.hpp
8485
src/angle-axis.hpp
8586
src/quaternion.hpp
8687
)

src/details.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <iostream>
2525

2626
#include "eigenpy/eigenpy.hpp"
27+
#include "eigenpy/registration.hpp"
2728
#include "eigenpy/exception.hpp"
2829
#include "eigenpy/map.hpp"
2930

@@ -206,6 +207,7 @@ namespace eigenpy
206207
template<typename MatType,typename EigenEquivalentType>
207208
void enableEigenPySpecific()
208209
{
210+
if(check_registration<MatType>()) return;
209211
numpy_import_array();
210212

211213
boost::python::to_python_converter<MatType,EigenToPy<MatType,MatType> >();

src/exception.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015 LAAS-CNRS
2+
* Copyright (c) 2015-2016 LAAS-CNRS
33
*
44
* This file is part of eigenpy.
55
* eigenpy is free software: you can redistribute it and/or
@@ -14,7 +14,8 @@
1414
* with eigenpy. If not, see <http://www.gnu.org/licenses/>.
1515
*/
1616

17-
#include <eigenpy/exception.hpp>
17+
#include "eigenpy/exception.hpp"
18+
#include "eigenpy/registration.hpp"
1819

1920

2021
namespace eigenpy
@@ -30,6 +31,8 @@ namespace eigenpy
3031

3132
void Exception::registerException()
3233
{
34+
if(check_registration<eigenpy::Exception>()) return;
35+
3336
pyType = boost::python::class_<eigenpy::Exception>
3437
("Exception",boost::python::init<std::string>())
3538
.add_property("message", &eigenpy::Exception::copyMessage)

src/registration.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016, Justin Carpentier, LAAS-CNRS
3+
*
4+
* This file is part of eigenpy.
5+
* eigenpy is free software: you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public License
7+
* as published by the Free Software Foundation, either version 3 of
8+
* the License, or (at your option) any later version.
9+
* eigenpy is distributed in the hope that it will be
10+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
11+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details. You should
13+
* have received a copy of the GNU Lesser General Public License along
14+
* with eigenpy. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#ifndef __eigenpy_registration_hpp__
18+
#define __eigenpy_registration_hpp__
19+
20+
#include <boost/python.hpp>
21+
22+
namespace eigenpy
23+
{
24+
///
25+
/// \brief Check at runtime the registration of the type T inside the boost python registry.
26+
///
27+
/// \tparam T The type to check the registration.
28+
///
29+
/// \returns true if the type T is already registered.
30+
///
31+
template<typename T>
32+
inline bool check_registration()
33+
{
34+
namespace bp = boost::python;
35+
36+
const bp::type_info info = bp::type_id<T>();
37+
const bp::converter::registration* reg = bp::converter::registry::query(info);
38+
if (reg == NULL) return false;
39+
else if ((*reg).m_to_python == NULL) return false;
40+
41+
return true;
42+
}
43+
}
44+
45+
#endif // ifndef __eigenpy_registration_hpp__

0 commit comments

Comments
 (0)