WIP: range wrapper + WIP: array IO

This commit is contained in:
Christian Zimmermann 2024-02-05 00:38:08 +01:00
parent c0fb3fe7ea
commit eeefff2cde
3 changed files with 113 additions and 0 deletions

View file

@ -17,6 +17,10 @@ namespace CNORXZ
virtual RangePtr range() const = 0;
virtual SizeT size() const = 0;
// virtual PyObject* get() const = 0; // operator[]!!
#ifdef HAVE_CEREAL
virtual void writeFile(Format f, const String& fname);
virtual void readFile(Format f, const String& fname);
#endif
};
template <typename T>

View file

@ -0,0 +1,41 @@
#ifndef __python_cnorxz_range_wrapper_h__
#define __python_cnorxz_range_wrapper_h__
#include <Python.h>
#include "cnorxz.h"
namespace CNORXZ
{
class CRangeWrapper
{
public:
DEFAULT_MEMBERS(CRangeWrapper);
inline bool _init() const { return mR != nullptr; }
inline SizeT size() const { return mR->size(); }
inline SizeT dim() const { return mR->dim(); }
inline String stringMeta(SizeT pos) const { mR->stringMeta(pos); }
private:
RangePtr mR;
};
}
struct PyCRange
{
PyObject_HEAD
CNORXZ::CRangeWrapper* ptrObj;
};
int PyCRange_init(PyCRange* self, PyObject* args, PyObject* kwds);
void PyCRange_dealloc(PyCRange* self);
PyObject* PyCRange_size(PyCRange* self);
PyObject* PyCRange_dim(PyCRange* self);
PyObject* PyCRange_stringMeta(PyCRange* self, PyObject* args, PyObject* kwds);
PyTypeObject* PyCRange_init();
extern PyMethodDef PyCRange_methods[];
extern PyTypeObject PyCRangeType;
#endif

View file

@ -0,0 +1,68 @@
#include "range_wrapper.h"
using namespace CNORXZ;
int PyCRange_init(PyCRange* self, PyObject* args, PyObject* kwds)
{
//!!!
}
void PyCRange_dealloc(PyCRange* self)
{
delete self->ptrObj;
Py_TYPE(self)->tp_free(self);
}
PyObject* PyCRange_size(PyCRange* self)
{
if(not self->ptrObj->_init()){
const SizeT retval = 0;
return Py_BuildValue("k", retval);
}
const SizeT retval = self->ptrObj->size();
return Py_BuildValue("k", retval);
}
PyObject* PyCRange_dim(PyCRange* self)
{
if(not self->ptrObj->_init()){
const SizeT retval = 0;
return Py_BuildValue("k", retval);
}
const SizeT retval = self->ptrObj->dim();
return Py_BuildValue("k", retval);
}
PyObject* PyCRange_stringMeta(PyCRange* self, PyObject* args, PyObject* kwds)
{
//!!!
}
PyMethodDef PyCRange_methods[] = {
{ "size", (PyCFunction) PyCRange_size, METH_VARARGS, "return range size" },
{ "dim", (PyCFunction) PyCRange_dim, METH_VARARGS, "return range dimension" },
{ "stringMeta", (PyCFunction) PyCRange_stringMeta, METH_VARARGS,
"return string meta data for given range position" },
{ NULL }
};
PyTypeObject PyCRangeType = { PyVarObject_HEAD_INIT(NULL,0) "cnorxz.CRange" };
PyTypeObject* PyCRangeType_init()
{
PyCRangeType.tp_new = PyType_GenericNew;
PyCRangeType.tp_basicsize = sizeof(PyCRange);
PyCRangeType.tp_dealloc = (destructor) PyCRange_dealloc;
PyCRangeType.to_flags = Py_TPFLAGS_DEFAULT;
PyCRangeType.tp_doc = "cnorxz CRange wrapper";
PyCRangeType.tp_methods = PyCRange_methods;
PyCRangeType.tp_init = (initproc) PyCRange_init;
if(PyType_Ready(&PyCRangeType) < 0){
return NULL;
}
else {
return &PyCRangeType;
}
}