@@ -35,31 +35,39 @@ namespace eigenpy
3535 template <> struct NumpyEquivalentType <int > { enum { type_code = NPY_INT };};
3636 template <> struct NumpyEquivalentType <float > { enum { type_code = NPY_FLOAT };};
3737
38+ namespace bp = boost::python;
39+
3840 struct PyMatrixType
3941 {
40- boost::python::object pyMatrixType;
41- boost::python::object pyModule;
4242
43- PyMatrixType ()
43+ static PyMatrixType & getInstance ()
4444 {
45- pyModule = boost::python::import ( " numpy " ) ;
46- pyMatrixType = pyModule. attr ( " matrix " ) ;
45+ static PyMatrixType instance ;
46+ return instance ;
4747 }
48- operator boost::python::object () { return pyMatrixType; }
4948
50- boost::python::object make (PyArrayObject* pyArray, bool copy = false )
49+ operator bp::object () { return pyMatrixType; }
50+
51+ bp::object make (PyArrayObject* pyArray, bool copy = false )
5152 { return make ((PyObject*)pyArray,copy); }
52- boost::python ::object make (PyObject* pyObj, bool copy = false )
53+ bp ::object make (PyObject* pyObj, bool copy = false )
5354 {
5455 boost::python::object m
55- = pyMatrixType ( boost::python::object (boost::python::handle<>(pyObj)),
56- boost::python::object (), copy );
56+ = pyMatrixType (bp::object (bp::handle<>(pyObj)), bp::object (), copy);
5757 Py_INCREF (m.ptr ());
5858 return m;
5959 }
60- };
6160
62- extern PyMatrixType pyMatrixType;
61+ protected:
62+ PyMatrixType ()
63+ {
64+ pyModule = boost::python::import (" numpy" );
65+ pyMatrixType = pyModule.attr (" matrix" );
66+ }
67+
68+ bp::object pyMatrixType;
69+ bp::object pyModule;
70+ };
6371
6472 /* --- TO PYTHON -------------------------------------------------------------- */
6573 template < typename MatType,typename EquivalentEigenType >
@@ -78,7 +86,7 @@ namespace eigenpy
7886
7987 MapNumpy<EquivalentEigenType>::map (pyArray) = mat;
8088
81- return pyMatrixType .make (pyArray).ptr ();
89+ return PyMatrixType::getInstance () .make (pyArray).ptr ();
8290 }
8391 };
8492
0 commit comments