Library of Assembled Shared Sources
pyobject_plus.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2025 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
44#define LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
45
46#include "python_common.h"
47#include "../meta/is_derived.h"
48
49/** @ingroup Python
50 * Place as first line of your Pythonized class.
51 *
52 * For @a t_parentClass use the C++ class from which you wish the python object inherits.
53 * @a t_parentClass must also be a Pythonized class or use lass::python::PyObjectPlus as default.
54 *
55 * @remark Any declarations coming after this macro are private!
56 */
57#define PY_HEADER( t_parentClass ) \
58 public: \
59 typedef t_parentClass _lassPyParentType; \
60 static ::lass::python::impl::ClassDefinition _lassPyClassDef; \
61 ::lass::python::impl::ClassDefinition* _lassPyGetClassDef() const override { return &_lassPyClassDef; } \
62 private:
63
64#include "class_definition.h"
65#include "export_traits.h"
66#include "pyobject_casters.h"
67
68namespace lass
69{
70 namespace python
71 {
72 /** @ingroup Python
73 */
74 template<typename T>
75 PyObject* pyBuildSimpleObject(T& iV)
76 {
77 LockGIL LASS_UNUSED(lock);
78 return PyExportTraits<T>::build(iV);
79 }
80
81 /** @ingroup Python
82 */
83 template<typename T>
84 PyObject* pyBuildSimpleObject(const T& iV)
85 {
86 LockGIL LASS_UNUSED(lock);
87 return PyExportTraits<T>::build(iV);
88 }
89
90 /** @ingroup Python
91 */
92 template<typename T, typename Deleter>
93 PyObject* pyBuildSimpleObject(std::unique_ptr<T, Deleter>&& iV)
94 {
95 LockGIL LASS_UNUSED(lock);
96 return PyExportTraits< std::unique_ptr<T, Deleter> >::build(std::move(iV));
97 }
98
99 /** @ingroup Python
100 */
101 template<typename T>
102 int pyGetSimpleObject(PyObject* iV, T& oV)
103 {
104 LockGIL LASS_UNUSED(lock);
105 return PyExportTraits<T>::get(iV,oV);
106 }
107
108 /** Helper to specialise PyExportTraits for enumerations.
109 * @ingroup PyExportTraits
110 *
111 * When you want to export enumeration types, you need to specialize PyExportTraits for it.
112 * This structure will help you
113 *
114 * @code
115 * enum MyEnumeration { meFoo, meBar, meFuz };
116 * namespace lass
117 * {
118 * namespace python
119 * {
120 * template <> struct PyExportTraits<MyEnumeration>: public PyExportTraitsEnum<MyEnumeration> {};
121 * }
122 * }
123 * @endcode
124 */
125 template <typename EnumType, typename IntegerType = std::underlying_type_t<EnumType>>
127 {
128 constexpr static const char* py_typing = "int";
129
130 static PyObject* build(const EnumType iv)
131 {
132 return pyBuildSimpleObject(static_cast<IntegerType>(iv));
133 }
134 static int get(PyObject* iv, EnumType& ov)
135 {
136 IntegerType temp;
137 if (pyGetSimpleObject(iv, temp) != 0)
138 {
139 return 1;
140 }
141 ov = static_cast<EnumType>(temp);
142 return 0;
143 }
144 };
145
146 /** PyObjectPlus. Base class for pythonable objects.
147 * @ingroup Python
148 * @author Tom De Muer
149 * @date 2003
150 * Usage:
151 * The usage will be given by providing an example. See the test case and look
152 * for the files bar.h and bar.cpp.
153 */
154 class LASS_PYTHON_DLL PyObjectPlus :
155 public PyObject
156 {
157 public:
158 static impl::ClassDefinition _lassPyClassDef;
159 static void _lassPyClassRegisterHook();
160 virtual impl::ClassDefinition* _lassPyGetClassDef() const { return &_lassPyClassDef; }
161 typedef void TCppClass;
162
163 PyObjectPlus();
164 virtual ~PyObjectPlus();
165
166 protected:
167 PyObjectPlus(const PyObjectPlus& other);
168 PyObjectPlus& operator=(const PyObjectPlus& other);
169 private:
170 };
171
172 /** meta function to detect if a type is a PyObject-derived type
173 * @ingroup Python
174 */
175 template <typename T> struct IsPyObject: meta::IsDerived<T, PyObject> {};
176
177 namespace impl
178 {
179 LASS_PYTHON_DLL bool LASS_CALL checkSequenceSize(PyObject* iValue, Py_ssize_t iExpectedSize);
180 LASS_PYTHON_DLL TPyObjPtr LASS_CALL checkedFastSequence(PyObject* obj);
181 LASS_PYTHON_DLL TPyObjPtr LASS_CALL checkedFastSequence(PyObject* obj, Py_ssize_t expectedSize);
182 LASS_PYTHON_DLL TPyObjPtr LASS_CALL checkedFastSequence(PyObject* obj, Py_ssize_t minimumSize, Py_ssize_t maximumSize);
183 LASS_PYTHON_DLL PyObject* LASS_CALL establishMagicalBackLinks(PyObject* result, PyObject* self);
184 }
185 }
186}
187
188#include "pyobject_plus.inl"
189
190#endif
static void _lassPyClassRegisterHook()
This function can be used to execute some code at injection time.
Definition of a Python class.
PyObjectPtr< PyObject >::Type TPyObjPtr
PyObjectPtr to a PyObject.
Library for Assembled Shared Sources.
Definition config.h:53
meta function to detect if a type is a PyObject-derived type
Helper to specialise PyExportTraits for enumerations.