When importing a Cython namespace and its corresponding functions how do you account for data types such as double

Viewed 24

I made a Cython script that imports the following from C++:

class types{
    public:
    //Tuple Declarations
        class Tuple: std::vector<std::vector<std::vector<int>>>{};
        class Tuple: std::vector<std::vector<std::vector<int[]>>>{};
        class Tuple: std::vector<std::vector<std::vector<std::string>>>{};
        class Tuple: std::vector<std::vector<std::vector<std::string[]>>>{};
        class Tuple: std::vector<std::vector<std::vector<double>>>{};
        class Tuple: std::vector<std::vector<std::vector<double[]>>>{};
        class Tuple: std::vector<std::vector<std::vector<char>>>{};
        class Tuple: std::vector<std::vector<std::vector<char[]>>>{};
        class Tuple: std::vector<std::vector<std::vector<float>>>{};
        class Tuple: std::vector<std::vector<std::vector<float[]>>>{};
        class Tuple: std::vector<std::vector<std::vector<void *>>>{};
        class Tuple: std::vector<std::vector<std::vector<void *[]>>>{};
        class Tuple: std::vector<std::vector<std::vector<std::decimal::decimal128>>>{};
        class Tuple: std::vector<std::vector<std::vector<std::decimal::decimal128[]>>>{};

This was going to be used for some rendering software I am creating in python and I used the following to import those functions:

from libcpp.string cimport string as std_string
cdef extern from * namespace "CustomRender":

cdef cppclass types:
    cppclass Tuple:
        Tuple(int arg)
        Tuple(double arg)
        Tuple(std_string arg)

cdef class TupleWrapper:
    cdef types.Tuple* nc
    def __dealloc__(self):
        del self.nc
@classmethod  # use staticmethod to make this cdef
def make_from_int(cls, int arg):
    cdef TupleWrapper o = cls.__new__(cls)
    o.nc = new types.Tuple(arg)
    return o

# and the same pattern for the int and string versions

cdef class NestedClassWrapper2:
    cdef types.Tuple* nc

def __dealloc__(self):
    del self.nc

def __cinit__(self, arg):
    if isinstance(arg, int):
        # cast to a C int
        self.nc = new types.Tuple(<int>arg)
    elif isinstance(arg, float):
        self.nc = new types.Tuple(<double>arg)
    elif isinstance(arg, bytes):
        self.nc = new types.Tuple(<std_string>arg)
    elif isinstance(arg,None):
        self.nc= new types.Tuple(<void>arg)
    else:
        raise TypeError

I was wondering for things like void should I use None and for decimal do I leave it as a float. Also will Cython support vectors?

0 Answers
Related