Verified Commit 382ca0a1 authored by Justin Carpentier's avatar Justin Carpentier
Browse files

core/register: make Register a pure singleton class

parent 9387d8ed
......@@ -21,14 +21,7 @@ namespace eigenpy
struct EIGENPY_DLLEXPORT Register
{
static PyArray_Descr * getPyArrayDescr(PyTypeObject * py_type_ptr)
{
MapDescr::iterator it = py_array_descr_bindings.find(py_type_ptr);
if(it != py_array_descr_bindings.end())
return it->second;
else
return NULL;
}
static PyArray_Descr * getPyArrayDescr(PyTypeObject * py_type_ptr);
template<typename Scalar>
static bool isRegistered()
......@@ -36,22 +29,9 @@ namespace eigenpy
return isRegistered(Register::getPyType<Scalar>());
}
static bool isRegistered(PyTypeObject * py_type_ptr)
{
if(getPyArrayDescr(py_type_ptr) != NULL)
return true;
else
return false;
}
static bool isRegistered(PyTypeObject * py_type_ptr);
static int getTypeCode(PyTypeObject * py_type_ptr)
{
MapCode::iterator it = py_array_code_bindings.find(py_type_ptr);
if(it != py_array_code_bindings.end())
return it->second;
else
return PyArray_TypeNum(py_type_ptr);
}
static int getTypeCode(PyTypeObject * py_type_ptr);
template<typename Scalar>
static PyTypeObject * getPyType()
......@@ -74,7 +54,7 @@ namespace eigenpy
return new_descr->typeobj;
}
}
template<typename Scalar>
static int getTypeCode()
{
......@@ -83,10 +63,10 @@ namespace eigenpy
else
{
const std::type_info & info = typeid(Scalar);
if(type_to_py_type_bindings.find(&info) != type_to_py_type_bindings.end())
if(instance().type_to_py_type_bindings.find(&info) != instance().type_to_py_type_bindings.end())
{
PyTypeObject * py_type = type_to_py_type_bindings[&info];
int code = py_array_code_bindings[py_type];
PyTypeObject * py_type = instance().type_to_py_type_bindings[&info];
int code = instance().py_array_code_bindings[py_type];
return code;
}
......@@ -103,48 +83,9 @@ namespace eigenpy
PyArray_NonzeroFunc * nonzero,
PyArray_CopySwapFunc * copyswap,
PyArray_CopySwapNFunc * copyswapn,
PyArray_DotFunc * dotfunc)
{
namespace bp = boost::python;
PyArray_Descr * descr_ptr = new PyArray_Descr(*call_PyArray_DescrFromType(NPY_OBJECT));
PyArray_Descr & descr = *descr_ptr;
descr.typeobj = py_type_ptr;
descr.kind = 'V';
descr.byteorder = '=';
descr.elsize = type_size;
descr.flags = NPY_LIST_PICKLE | NPY_USE_GETITEM | NPY_USE_SETITEM | NPY_NEEDS_INIT | NPY_NEEDS_PYAPI;
// descr->names = PyTuple_New(0);
// descr->fields = PyDict_New();
PyArray_ArrFuncs * funcs_ptr = new PyArray_ArrFuncs;
PyArray_ArrFuncs & funcs = *funcs_ptr;
descr.f = funcs_ptr;
call_PyArray_InitArrFuncs(funcs_ptr);
funcs.getitem = getitem;
funcs.setitem = setitem;
funcs.nonzero = nonzero;
funcs.copyswap = copyswap;
funcs.copyswapn = copyswapn;
funcs.dotfunc = dotfunc;
// f->cast = cast;
const int code = call_PyArray_RegisterDataType(descr_ptr);
assert(code >= 0 && "The return code should be positive");
PyArray_Descr * new_descr = call_PyArray_DescrFromType(code);
type_to_py_type_bindings.insert(std::make_pair(type_info_ptr,py_type_ptr));
py_array_descr_bindings[py_type_ptr] = new_descr;
py_array_code_bindings[py_type_ptr] = code;
// PyArray_RegisterCanCast(descr,NPY_OBJECT,NPY_NOSCALAR);
return code;
}
PyArray_DotFunc * dotfunc);
// static Register & instance()
// {
// return self;
// }
static Register & instance();
private:
......@@ -167,16 +108,14 @@ namespace eigenpy
};
typedef std::map<const std::type_info *,PyTypeObject *,Compare_TypeInfo> MapInfo;
static MapInfo type_to_py_type_bindings;
MapInfo type_to_py_type_bindings;
typedef std::map<PyTypeObject *,PyArray_Descr *,Compare_PyTypeObject> MapDescr;
static MapDescr py_array_descr_bindings;
MapDescr py_array_descr_bindings;
typedef std::map<PyTypeObject *,int,Compare_PyTypeObject> MapCode;
static MapCode py_array_code_bindings;
// static Register self;
MapCode py_array_code_bindings;
};
} // namespace eigenpy
......
......@@ -7,8 +7,80 @@
namespace eigenpy
{
Register::MapDescr Register::py_array_descr_bindings;
Register::MapCode Register::py_array_code_bindings;
Register::MapInfo Register::type_to_py_type_bindings;
PyArray_Descr * Register::getPyArrayDescr(PyTypeObject * py_type_ptr)
{
MapDescr::iterator it = instance().py_array_descr_bindings.find(py_type_ptr);
if(it != instance().py_array_descr_bindings.end())
return it->second;
else
return NULL;
}
bool Register::isRegistered(PyTypeObject * py_type_ptr)
{
if(getPyArrayDescr(py_type_ptr) != NULL)
return true;
else
return false;
}
int Register::getTypeCode(PyTypeObject * py_type_ptr)
{
MapCode::iterator it = instance().py_array_code_bindings.find(py_type_ptr);
if(it != instance().py_array_code_bindings.end())
return it->second;
else
return PyArray_TypeNum(py_type_ptr);
}
int Register::registerNewType(PyTypeObject * py_type_ptr,
const std::type_info * type_info_ptr,
const int type_size,
PyArray_GetItemFunc * getitem,
PyArray_SetItemFunc * setitem,
PyArray_NonzeroFunc * nonzero,
PyArray_CopySwapFunc * copyswap,
PyArray_CopySwapNFunc * copyswapn,
PyArray_DotFunc * dotfunc)
{
PyArray_Descr * descr_ptr = new PyArray_Descr(*call_PyArray_DescrFromType(NPY_OBJECT));
PyArray_Descr & descr = *descr_ptr;
descr.typeobj = py_type_ptr;
descr.kind = 'V';
descr.byteorder = '=';
descr.elsize = type_size;
descr.flags = NPY_LIST_PICKLE | NPY_USE_GETITEM | NPY_USE_SETITEM | NPY_NEEDS_INIT | NPY_NEEDS_PYAPI;
// descr->names = PyTuple_New(0);
// descr->fields = PyDict_New();
PyArray_ArrFuncs * funcs_ptr = new PyArray_ArrFuncs;
PyArray_ArrFuncs & funcs = *funcs_ptr;
descr.f = funcs_ptr;
call_PyArray_InitArrFuncs(funcs_ptr);
funcs.getitem = getitem;
funcs.setitem = setitem;
funcs.nonzero = nonzero;
funcs.copyswap = copyswap;
funcs.copyswapn = copyswapn;
funcs.dotfunc = dotfunc;
// f->cast = cast;
const int code = call_PyArray_RegisterDataType(descr_ptr);
assert(code >= 0 && "The return code should be positive");
PyArray_Descr * new_descr = call_PyArray_DescrFromType(code);
instance().type_to_py_type_bindings.insert(std::make_pair(type_info_ptr,py_type_ptr));
instance().py_array_descr_bindings[py_type_ptr] = new_descr;
instance().py_array_code_bindings[py_type_ptr] = code;
// PyArray_RegisterCanCast(descr,NPY_OBJECT,NPY_NOSCALAR);
return code;
}
Register & Register::instance()
{
static Register self;
return self;
}
} // namespace eigenpy
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment