WIP cpp array wrapper

This commit is contained in:
Christian Zimmermann 2023-05-23 00:48:01 +02:00
parent c155319b59
commit 4d2e9fc6e7
2 changed files with 85 additions and 0 deletions

View file

@ -0,0 +1,39 @@
#include <Python.h>
#include "cnorxz.h"
namespace CNORXZ
{
class CArrayWrapperBase
{
public:
DEFAULT_MEMBERS(ArrayWrapperBase);
virtual ~CArrayWrapperBase() = default;
virtual RangePtr range() const = 0;
virtual SizeT size() const = 0;
// virtual PyObject* get() const = 0; // operator[]!!
};
template <typename T>
class CArrayWrapper : public CArrayWrapperBase
{
private:
Sptr<CArrayBase<T>> mArr;
public:
DEFAULT_MEMBERS(ArrayWrapper);
CArrayWrapper(const RangePtr& r) : mArr( std::make_shared<MArray<T>>(r) ) {}
virtual RangePtr range() const override final { return mArr->range(); }
virtual SizeT size() const override final { return mArr->size(); }
};
}
struct PyCArrayB
{
PyObject_HEAD
CNORXZ::CArrayWrapperBase* ptrObj;
};

View file

@ -0,0 +1,46 @@
#include "cnorxz/core/include/array_wrapper.h"
using namespace CNORXZ
static int PyCArrayB_init(CArrayWrapperBase* self, PyObject* args, PyObject* kwds)
{
static char* kwlist[] = { "type", "extension" , NULL };
SizeT ext = 0;
const char* type;
if(not PyArg_ParseTupleAndKeywords(args, kwds, "s|k", kwlist, &type, &ext)){
return NULL;
}
if(ext != 0){
RangePtr r = CRangeFactory(ext).create();
self->ptrObj = new CArrayBase<T>(r);
}
else {
self->ptrObj = new CArrayBase<T>();
}
return 0;
}
static void PyCArrayB_dealloc(CArrayWrapperBase* self)
{
delete self->ptrObject;
Py_TYPE(self)->tp_free(self);
}
static PyObject* PyCArrayB_size(CArrayWrapperBase* self)
{
if(not self->ptrObj->range()){
PyErr_SetString(PyExc_RuntimeError, "array not initialized");
return NULL;
}
const SizeT retval = self->ptrObj->size();
return Py_BuildValue("k",retval);
}
static PyMethodDef PyCArrayB_methods[] = {
{ "size", (PyCFunction)PyCArrayB_size, METH_VARARGS, "return size of the array" },
{ NULL }
};