I am in the final stages of rewriting a C++ Python wrapper.
In Python everything is a PyObject
, and I have an Object
class wrapping a PyObject*
.
I've provided []
access. If the underlying PyObject
is a dictionary, ob["key"]
will retrieve its value, on the other hand if it is a sequence type, ob[42] retrieves the value.
I now want to provide for(auto x:X)
type syntax.
The original project (PyCXX) comprised a base Object
class, and for each Python Type derived an associated class.
Sequence types (list,tuple, set) would derive from a Sequence
class, and dictionary would have a Dict
class.
The Sequence
class contains the machinery for a stdlib vector type container (code here). Similarly, the Dict
class contains the machinery for a std::map type container (code here).
I apologise that the code is messy -- in due course I will clean it up and present it at the bottom of this question.
I would like to attempt something ambitious -- to provide functionality for both of these container types within the same Object
class.
So, if the underlying PyObject
is a sequence type, we can do things like: for(auto x:X)
, and similarly we can apply a stdlib sorting algorithm for a dictionary type.
I would like to know in advance whether this is possible, practical, ill-advised, etc.
I'm not worried about designing something that if used improperly would crash or cause undefined behaviour. My concern is to design something that if used properly provides an effortless bridge between C++ and Python.
If this is indeed possible, might someone be willing to draft a basic roadmap?
If it is impossible, a clear exposition of why would help me towards a practical ultimate design.
Aucun commentaire:
Enregistrer un commentaire