Struct wrapping
Wrapping D's structs is similar to wrapping classes. In fact, many of the operations are identical.
void wrap_struct(T, char[] structname = symbolnameof!(T), Params...) ();
- T is the struct being wrapped.
- structname is the name of the struct as it will appear in Python.
- Params is a series of struct types (defined below), which define the various members of the struct.
As with calls to wrap_class
, calls to wrap_struct
must occur after calling module_init
.
To expose the data members, member functions, and properties of the struct, you must pass a series of struct template instantiations to wrap_struct
.
struct Member(char[] realname, char[] name=realname);
- This exposes a data member of the struct to Python. The member must be a convertible type. The realname is the member's actual name. name is the name of the data member as it will be used in Python. This defaults to realname.
struct Def(alias fn, char[] name = symbolnameof!(fn), fn_t = typeof(&fn));
- This wraps a member function of the struct. It is in fact exactly the same
Def
struct template used to wrap class methods, including the lack of support for default arguments. struct StaticDef(alias fn, char[] name = symbolnameof!(fn), fn_t = typeof(&fn), uint MIN_ARGS = minArgs!(fn));
- This wraps a static member function of the struct. It is the same
StaticDef
struct template used to wrap static class member functions, and also includes support for default arguments. struct Property(alias fn, char[] name = symbolnameof!(fn), bool RO = false);
- This wraps a property. It is the same
Property
struct template used to wrap class properties. struct Repr(alias fn);
- This allows you to expose a member function of the struct as the Python type's
__repr__
function. The member function must have the signaturechar[] function()
. It is the sameRepr
struct template used when wrapping classes. struct Iter(iter_t);
- This allows the user to specify a different overload of opApply than the default. (The default is always the one that is lexically first.) It is the same
Iter
struct template used in class wrapping. struct AltIter(alias fn, char[] name = symbolnameof!(fn), iter_t = implementationDetail);
- This wraps alternate iterator methods as Python methods that return iterator objects. It is the same
AltIter
struct template used in class wrapping.
The is_wrapped
template is available for wrapped structs, just like it is for wrapped classes.
It is important to note that wrapping a struct S
makes both S
itself and S*
available as convertible types.
Automatic operator overloading
Support for operator overloading in structs is identical to that available for classes.
Inheritance
D does not support struct inheritance. Therefore, Pyd does not provide any support for struct inheritance. However, the Python type wrapping the D struct can be subclassed from within Python. Users should not expect polymorphic behavior if they attempt to pass instances of any subclasses back to D.
Examples
(Todo.)