Export C++ classes that don't derive from lass::python::PyObjectPlus.
Shadow classes are LASS' wrapper classes that allow you to export C++ classes that don't derive from lass::python::PyObjectPlus to Python.
They are defined by invoking two macros:
Both macros are typically invoked in a header file (not necessarily the one that defines the original C++ classes), as both macros must be seen by all exports that use this class as parameter or return type.
Once you have defined the Shadow class, you use the regular macros like PY_DECLARE_CLASS_NAME or PY_CLASS_METHOD to fully define the Python export for this class. But instead of using the C++ class as first argument, you use the shadow class.
Here's a quick comparison between both techniques:
Shadow-classes need a pointer type to store their shadowee instances. This is defined by the pointer traits chosen for the shadow class.
By default, the SharedPointerTraits is used with util::SharedPtr as shadowee pointer, but you can specify a custom one using PY_SHADOW_CLASS_PTRTRAITS.
Available pointer traits are:
To make your custom pointer traits, copy the pattern of these three.
id must return an ID that uniquely identifies a shadowee instance for as long as it is alive and at least one shadow pointer references it (when it's removed from the shadow cache). Return 0 to opt-out from the shadow cache. Classes | |
| struct | lass::python::SharedPointerTraits< T, S, C > |
| Pointer-traits for Python Shadow classes that use lass::util::SharedPtr for storage. More... | |
| struct | lass::python::NakedPointerTraits< T > |
Pointer-traits for Python Shadow classes that use raw * pointers for storage. More... | |
| struct | lass::python::StdSharedPointerTraits< T > |
Pointer-traits for Python Shadow classes that use std::shared_ptr for storage. More... | |
Macros | |
| #define | PY_SHADOW_CLASS_EX(dllInterface, i_PyObjectShadowClass, t_CppClass, t_PyObjectParent, t_PointerTraits) |
| Declare Python shadow class with full control. | |
| #define | PY_SHADOW_CLASS_PTRTRAITS(dllInterface, i_PyObjectShadowClass, t_CppClass, pointerTraits) |
| Declare Python shadow class with custom pointer traits. | |
| #define | PY_SHADOW_CLASS(dllInterface, i_PyObjectShadowClass, t_CppClass) |
| Declare Python shadow class with util::SharedPtr as default shadowee pointer type. | |
| #define | PY_SHADOW_CLASS_DERIVED(dllInterface, i_PyObjectShadowClass, t_CppClass, t_PyObjectShadowParent) |
| Declare Python shadow child class with a parent. | |
| #define | PY_SHADOW_CLASS_NOCONSTRUCTOR_EX(dllInterface, i_PyObjectShadowClass, t_CppClass, t_PyObjectBase, t_PyObjectParent) |
| Deprecated alias for PY_SHADOW_CLASS_EX. | |
| #define | PY_SHADOW_CLASS_NOCONSTRUCTOR(dllInterface, i_PyObjectShadowClass, t_CppClass) |
| Deprecated alias for PY_SHADOW_CLASS. | |
| #define | PY_WEAK_SHADOW_CLASS(dllInterface, i_PyObjectShadowClass, t_CppClass) |
| Deprecated alias for PY_SHADOW_CLASS. | |
| #define | PY_WEAK_SHADOW_CLASS_NOCONSTRUCTOR(dllInterface, i_PyObjectShadowClass, t_CppClass) |
| Deprecated alias for PY_SHADOW_CLASS. | |
| #define | PY_SHADOW_CLASS_ENABLE_AUTOMATIC_INVALIDATION(i_PyObjectShadowClass) |
| Deprecated. | |
| #define | PY_SHADOW_CLASS_DERIVED_NOCONSTRUCTOR(dllInterface, i_PyObjectShadowClass, t_CppClass, t_PyObjectShadowParent) |
| Deprecated alias for PY_SHADOW_CLASS_DERIVED. | |
| #define | PY_SHADOW_CASTERS(t_ShadowObject) |
| Define lass::python::ShadoweeTraits for shadow class. | |
| #define | PY_SHADOW_DOWN_CASTERS(t_ShadowObject) |
| Deprecated alias for PY_SHADOW_CASTERS. | |
| #define | PY_SHADOW_DOWN_CASTERS_NOCONSTRUCTOR(t_ShadowObject) |
| Deprecated alias for PY_SHADOW_CASTERS. | |
| #define | PY_CLASS_CONVERTOR_EX(t_ShadowObject, f_conversionFunction, i_uniqueName) |
| Add implicit conversion function to Python class. | |
| #define | PY_CLASS_CONVERTOR(i_ShadowObject, t_sourceType) |
| Add implicit auto-conversion to Python class. | |
Typedefs | |
| using | lass::python::TShadoweeID = num::TuintPtr |
| ID-type to uniquely identify shadowee instances in the shadow cache. | |
| template<typename ShadoweeType> | |
| using | lass::python::ShadoweePtr |
| Helper to get the pointer type holding the shadowee in a shadow object. | |
| #define PY_SHADOW_CLASS_EX | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass, | |||
| t_PyObjectParent, | |||
| t_PointerTraits ) |
Declare Python shadow class with full control.
| dllInterface | desired DLL-interface of shadow class |
| i_PyObjectShadowClass | Unique C++ class identifier (unqualified name) of shadow class |
| t_CppClass | typename of the shadowee, the native C++ class being shadowed |
| t_PyObjectParent | the shadow's class parent (either another shadow class or lass::python::PyObjectPlus), this will be the base class of the Python type. |
| t_PointerTraits | complete type of pointer-traits to be used for shadowee storage. |
Definition at line 845 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS_PTRTRAITS | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass, | |||
| pointerTraits ) |
Declare Python shadow class with custom pointer traits.
The pointer-traits define how shadowee classes will be stored as pointer in the shadow class. Only specify the pointer-traits template name, the macro will add t_CppClass as template argument.
| dllInterface | desired DLL-interface of shadow class |
| i_PyObjectShadowClass | Unique C++ class identifier (unqualified name) of shadow class |
| t_CppClass | typename of the shadowee, the native C++ class being shadowed |
| pointerTraits | shadowee pointer-traits template name |
Definition at line 870 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass ) |
Declare Python shadow class with util::SharedPtr as default shadowee pointer type.
| dllInterface | desired DLL-interface of shadow class |
| i_PyObjectShadowClass | Unique C++ class identifier (unqualified name) of shadow class |
| t_CppClass | typename of the shadowee, the native C++ class being shadowed |
Definition at line 880 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS_DERIVED | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass, | |||
| t_PyObjectShadowParent ) |
Declare Python shadow child class with a parent.
Register a derived shadow class to reflect the same polymorphic class hierarchy in Python as in C++. If Ham derives from Spam, then a SharedPtr<Spam> pointer that contains a Ham instance will properly be returned as a Ham object in Python.
Derived shadow classes use the same pointer traits as the parent class, so you don't need to specify it again.
| dllInterface | desired DLL-interface of shadow class |
| i_PyObjectShadowClass | Unique C++ class identifier (unqualified name) of shadow class |
| t_CppClass | typename of the shadowee, the native C++ class being shadowed |
| t_PyObjectShadowParent | the parent's shadow class, this will be the base class of the Python type. |
Definition at line 898 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS_NOCONSTRUCTOR_EX | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass, | |||
| t_PyObjectBase, | |||
| t_PyObjectParent ) |
Deprecated alias for PY_SHADOW_CLASS_EX.
Definition at line 905 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS_NOCONSTRUCTOR | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass ) |
Deprecated alias for PY_SHADOW_CLASS.
Definition at line 912 of file pyshadow_object.h.
| #define PY_WEAK_SHADOW_CLASS | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass ) |
Deprecated alias for PY_SHADOW_CLASS.
Definition at line 919 of file pyshadow_object.h.
| #define PY_WEAK_SHADOW_CLASS_NOCONSTRUCTOR | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass ) |
Deprecated alias for PY_SHADOW_CLASS.
Definition at line 926 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS_ENABLE_AUTOMATIC_INVALIDATION | ( | i_PyObjectShadowClass | ) |
Deprecated.
Definition at line 933 of file pyshadow_object.h.
| #define PY_SHADOW_CLASS_DERIVED_NOCONSTRUCTOR | ( | dllInterface, | |
| i_PyObjectShadowClass, | |||
| t_CppClass, | |||
| t_PyObjectShadowParent ) |
Deprecated alias for PY_SHADOW_CLASS_DERIVED.
Definition at line 939 of file pyshadow_object.h.
| #define PY_SHADOW_CASTERS | ( | t_ShadowObject | ) |
Define lass::python::ShadoweeTraits for shadow class.
This ensures that shadowee class becomes usable as parameter or return type. If PySpam is t_ShadowObject, the shadow class of Spam, then Spam, const Spam&, Spam*, lass::util::SharedPtr<Spam>, etc. all become usable as parameter or return types.
| t_ShadowObject | fully qualified typename of Python Shadow class |
Definition at line 961 of file pyshadow_object.h.
| #define PY_SHADOW_DOWN_CASTERS | ( | t_ShadowObject | ) |
Deprecated alias for PY_SHADOW_CASTERS.
Definition at line 982 of file pyshadow_object.h.
| #define PY_SHADOW_DOWN_CASTERS_NOCONSTRUCTOR | ( | t_ShadowObject | ) |
Deprecated alias for PY_SHADOW_CASTERS.
Definition at line 989 of file pyshadow_object.h.
| #define PY_CLASS_CONVERTOR_EX | ( | t_ShadowObject, | |
| f_conversionFunction, | |||
| i_uniqueName ) |
Add implicit conversion function to Python class.
Add a conversion function to a Python class to implicitly convert a Python object to the C++ class when it is being passed as a parameter to a C++ function
By default, you need to convert types by explicitly constructing an object in Python, or you must overload the function on different types. By adding implicit convertors, they will automatically be attempted when calling functions taking the native C++ class. If conversion is successful, the function is called.
The conversion function must be of the following signature: int f(PyObject* obj, ShadoweePtr<T>& val). It receives a pointer to the Python object for which the conversion must be attempted. If successful, you should construct a new instance on the heap of the native C++ class, assign it to the shadowee pointer, and return 0. If failed, you should return 1, and a generic TypeError will be set.
Conversions are attempted in the order of registration.
This macro must be invoked in the same translation unit (.cpp file) as `PY_DECLARE_CLASS_`.
| t_ShadowObject | Python class (fully qualified) on which to add the convertor |
| f_conversionFunction | conversion function |
| i_uniqueName | identifier used to name the generated registration hook |
Definition at line 1055 of file pyshadow_object.h.
| #define PY_CLASS_CONVERTOR | ( | i_ShadowObject, | |
| t_sourceType ) |
Add implicit auto-conversion to Python class.
Add an implicit convertor from t_sourceType to the Python class i_ShadowObject. It is attempted when a Python object that is not already an instance of that class is passed to a C++ function taking the C++ class as a parameter: the object is first converted to an instance of t_sourceType, from which a new instance of the C++ class is constructed. This assumes the C++ class has a matching constructor.
Without it, you must convert the value explicitly in Python by constructing an instance of the Python class, or the C++ function must be overloaded on t_sourceType.
Conversions are attempted in the order of registration.
This macro must be invoked in the same translation unit (.cpp file) as `PY_DECLARE_CLASS_`.
| i_ShadowObject | Python class identifier (unqualified) on which to add the auto-convertor |
| t_sourceType | type from which to convert. It must be default-constructible and be exported to Python as a class or with PyExportTraits. |
Definition at line 1107 of file pyshadow_object.h.
| using lass::python::ShadoweePtr |
Helper to get the pointer type holding the shadowee in a shadow object.
| ShadoweeType | native C++ class, either the shadowee type being wrapped by a shadow class, or a direct exported type that derives from lass::python::PyObjectPlus. |
This helper gives you the correct pointer type to store a C++ class on the heap (either a shadowee type, or a direct Python class) that is compatible with the defined Python exports
If ShadoweeType is a shadowee type, this will be the pointer type that holds the shadowee object in the shadow object, and it's defined by the pointer traits passed to PY_SHADOW_CLASS_EX() or PY_SHADOW_CLASS_PTRTRAITS() (or SharedPointerTraits<ShadoweeType> if you use the default PY_SHADOW_CLASS() ). Depending on the constness of the shadowee, you will either get a pointer to a non-const ShadoweeType or a pointer to a const ShadoweeType.
If ShadoweeType is not a shadowed type, but is instead a direct export and derives directly or indirectly from lass::python::PyObjectPlus, then the pointer type is simply lass::python::PyObjectPtr<ShadoweeType>::Type.
Definition at line 826 of file pyshadow_object.h.