Giter Site home page Giter Site logo

Wrapping `struct`s about autowrap HOT 5 OPEN

openms avatar openms commented on August 16, 2024
Wrapping `struct`s

from autowrap.

Comments (5)

jpfeuffer avatar jpfeuffer commented on August 16, 2024

Hi @jakirkham !

Yes, sure, that is definitely something that we would consider.
What functionality are you expecting from autowrapping them? What can Cython do already internally?

from autowrap.

jakirkham avatar jakirkham commented on August 16, 2024

Thanks for your reply Julianus! 😄

Maybe some kind of Python object that can easily move between Python/C with attribute access covered via propertys in Python and struct field in C.

I guess C++ works with structs as if they were C++ classes except by default things declared in them are public as opposed to private. Maybe this in some ways makes them closer to what is already done here for classes with some tweaks?

AFAIK Cython doesn't do any wrapping on its own in this area. There was some discussion in issue ( cython/cython#3434 ) that seemed relevant.

A starting point might be extension types. For example something like this...

cdef class MyStruct:
    cdef double d
    cdef long i

...would get translated into something like this, which is pretty close.

struct __pyx_obj_<...>_MyStruct {
  PyObject_HEAD
  double d;
  long i;
};

The one tricky thing with this kind of approach is this is effectively declaring a new struct though wrapped code would already define a struct. It is also adding in another member.

So maybe one could make an extension type that holds a struct. Then one would need to generate some code to add property access of the fields.

There are probably other options I'm not thinking of

from autowrap.

jakirkham avatar jakirkham commented on August 16, 2024

Do you have any thoughts on this @jpfeuffer? 🙂

from autowrap.

jpfeuffer avatar jpfeuffer commented on August 16, 2024

Hi! So I thought a bit more about this.

The problem is a bit, that we have no experience at all with wrapping C in autowrap (it might work to some extend).
Was your question about structs in C or C-style structs without member functions in C++?

For C++ I do not yet see any benefits over just using cdef cppclass for a struct (based on the usecases we had so far) but you might have more experience.

The way autowrap wraps classes/structs is to create a Python class that has a member inst which holds a shared pointer
to the actual C++ object. Attribute access and functions are just forwarded to the underlying object then.
The nice thing about this is, that if this Python object is passed to a function, there is no overhead in copying data (if the original C++ function was expecting references or pointers).

We are also using and forwarding whatever constructor is provided in C(++). So the special init methods that python generates for dataclasses does not add any advantage either.

from autowrap.

jpfeuffer avatar jpfeuffer commented on August 16, 2024

@jakirkham Does that help? We just recently adopted this project and we are still learning about the internals of Cython. So feel free to correct us or suggest something different.

from autowrap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.