Skip to content

Commit 4f6f704

Browse files
committed
core: factorization of the code
1 parent 34d530d commit 4f6f704

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

include/eigenpy/eigen-from-python.hpp

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414

1515
namespace boost { namespace python { namespace converter {
1616

17-
/// \brief Template specialization of rvalue_from_python_data
18-
template<typename Derived>
19-
struct rvalue_from_python_data<Eigen::MatrixBase<Derived> const & >
20-
: rvalue_from_python_storage<Derived const & >
17+
template<typename MatrixReference>
18+
struct rvalue_from_python_data_eigen
19+
: rvalue_from_python_storage<MatrixReference>
2120
{
22-
typedef Eigen::MatrixBase<Derived> const & T;
21+
typedef MatrixReference T;
2322

2423
# if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \
2524
&& (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \
@@ -30,64 +29,56 @@ namespace boost { namespace python { namespace converter {
3029
# endif
3130

3231
// The usual constructor
33-
rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1)
32+
rvalue_from_python_data_eigen(rvalue_from_python_stage1_data const & _stage1)
3433
{
3534
this->stage1 = _stage1;
3635
}
3736

3837
// This constructor just sets m_convertible -- used by
3938
// implicitly_convertible<> to perform the final step of the
4039
// conversion, where the construct() function is already known.
41-
rvalue_from_python_data(void* convertible)
40+
rvalue_from_python_data_eigen(void* convertible)
4241
{
4342
this->stage1.convertible = convertible;
4443
}
4544

4645
// Destroys any object constructed in the storage.
47-
~rvalue_from_python_data()
46+
~rvalue_from_python_data_eigen()
4847
{
48+
typedef typename boost::remove_const<typename boost::remove_reference<MatrixReference>::type>::type MatrixType;
4949
if (this->stage1.convertible == this->storage.bytes)
50-
static_cast<Derived *>((void *)this->storage.bytes)->~Derived();
50+
static_cast<MatrixType *>((void *)this->storage.bytes)->~MatrixType();
5151
}
5252
};
5353

54+
55+
#define RVALUE_FROM_PYTHON_DATA_INIT(type) \
56+
typedef rvalue_from_python_data_eigen<type> Base; \
57+
\
58+
rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1) \
59+
: Base(_stage1) \
60+
{} \
61+
\
62+
rvalue_from_python_data(void* convertible) : Base(convertible) {};
63+
5464
/// \brief Template specialization of rvalue_from_python_data
5565
template<typename Derived>
56-
struct rvalue_from_python_data<Eigen::EigenBase<Derived> const & >
57-
: rvalue_from_python_storage<Derived const & >
66+
struct rvalue_from_python_data<Eigen::MatrixBase<Derived> const &>
67+
: rvalue_from_python_data_eigen<Derived const &>
5868
{
59-
typedef Eigen::EigenBase<Derived> const & T;
60-
61-
# if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \
62-
&& (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \
63-
&& (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \
64-
&& !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */
65-
// This must always be a POD struct with m_data its first member.
66-
BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage<T>,stage1) == 0);
67-
# endif
68-
69-
// The usual constructor
70-
rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1)
71-
{
72-
this->stage1 = _stage1;
73-
}
74-
75-
// This constructor just sets m_convertible -- used by
76-
// implicitly_convertible<> to perform the final step of the
77-
// conversion, where the construct() function is already known.
78-
rvalue_from_python_data(void* convertible)
79-
{
80-
this->stage1.convertible = convertible;
81-
}
82-
83-
// Destroys any object constructed in the storage.
84-
~rvalue_from_python_data()
85-
{
86-
if (this->stage1.convertible == this->storage.bytes)
87-
static_cast<Derived *>((void *)this->storage.bytes)->~Derived();
88-
}
69+
RVALUE_FROM_PYTHON_DATA_INIT(Derived const &)
8970
};
9071

72+
/// \brief Template specialization of rvalue_from_python_data
73+
template<typename Derived>
74+
struct rvalue_from_python_data<Eigen::EigenBase<Derived> const &>
75+
: rvalue_from_python_data_eigen<Derived const &>
76+
{
77+
RVALUE_FROM_PYTHON_DATA_INIT(Derived const &)
78+
};
79+
80+
#undef RVALUE_FROM_PYTHON_DATA_INIT
81+
9182
} } }
9283

9384
namespace eigenpy
@@ -317,8 +308,8 @@ namespace eigenpy
317308
&EigenFromPy<MatType>::construct,bp::type_id<MatType>());
318309
}
319310
};
320-
321311
#endif
312+
322313
}
323314

324315
#endif // __eigenpy_eigen_from_python_hpp__

0 commit comments

Comments
 (0)