Library of Assembled Shared Sources
pycallback.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_PYCALLBACK_H
44#define LASS_GUARDIAN_OF_INCLUSION_UTIL_PYCALLBACK_H
45
46#include "python_common.h"
47#include "pyshadow_object.h"
48#include "pyiteratorrange.h"
49#include "container.h"
50#include "pyobject_util.h"
51#include <map>
53#include "../meta/type_traits.h"
54
56#include "callback_python.h"
57
58namespace lass
59{
60namespace python
61{
62
63class MultiCallback;
64
65namespace impl
66{
67
68 class LASS_PYTHON_DLL MultiCallbackImplBase
69 {
70 public:
71 virtual ~MultiCallbackImplBase() {};
72 virtual void reset() = 0;
73 virtual Py_ssize_t length() const = 0;
74 virtual const std::type_info& type() const = 0;
75 virtual void* raw(bool writable) = 0;
76 virtual void call(const python::TPyObjPtr& args, PyObject* self) = 0;
77 virtual void add(const python::TPyObjPtr& args) = 0;
78
79 const std::string repr() const;
80 };
81
82
83 template <typename Callback>
84 class MultiCallbackImpl : public MultiCallbackImplBase
85 {
86 public:
87 typedef Callback TCallback;
88 typedef util::SharedPtr<Callback> TCallbackPtr;
89 typedef util::SharedPtr<const Callback> TConstCallbackPtr;
90
91 MultiCallbackImpl( const TCallbackPtr & callback, bool readonly) : callback_(callback), readOnly_(readonly)
92 {
93 }
94
95 void reset() override { return callback_->reset(); } // move to traits class, to discern multi and non-multi
96 Py_ssize_t length() const override { return static_cast<Py_ssize_t>(callback_->size()); }; // move to traits class, to discern multi and non-multi
97
98 const std::type_info& type() const override
99 {
100 return typeid(TCallbackPtr);
101 }
102 void* raw(bool writable) override
103 {
104 if (writable && readOnly_)
105 {
106 return 0;
107 }
108 return &callback_;
109 }
110 void call(const python::TPyObjPtr& args, PyObject* self) override;
111 void add(const python::TPyObjPtr& args) override
112 {
113 // the "thing" we add must be convertible to a callback
114 // [TODO] use a decodeTuple
115 LockGIL LASS_UNUSED(lock);
116 Callback tempCallback;
117 if (args.get() && PyTuple_Check(args.get()) && PyTuple_Size(args.get())==1)
118 {
119 Py_XINCREF(args.get());
120 Py_XINCREF(PyTuple_GetItem(args.get(),0));
121 int rv = pyGetSimpleObject(PyTuple_GetItem(args.get(),0),tempCallback);
122 if (rv)
123 {
124 impl::fetchAndThrowPythonException(LASS_PRETTY_FUNCTION);
125 }
126 for (size_t i=0;i<tempCallback.size();++i)
127 callback_->add(tempCallback[i]);
128 }
129 }
130 protected:
131 TCallbackPtr callback_;
132 bool readOnly_;
133 };
134}
135
136
137
138class MultiCallback;
139typedef PyObjectPtr<MultiCallback>::Type TMultiCallbackPtr;
140
141/** MultiCallback. Object for interfacing MultiCallbacks with Python
142*/
143class LASS_PYTHON_DLL MultiCallback : public PyObjectPlus, util::NonCopyable
144{
145 PY_HEADER(PyObjectPlus)
146public:
147 template <typename CallbackType> MultiCallback( const util::SharedPtr<CallbackType>& callback)
148 {
149 // [TODO] untested
150 LASS_THROW("unsupported");
151 TPimpl pimpl(
152 new impl::MultiCallbackImpl<CallbackType>(callback,false));
153 init(std::move(pimpl));
154 }
155 template <typename CallbackType> MultiCallback( const util::SharedPtr<const CallbackType>& /*callback*/)
156 {
157 // [TODO] untested
158 LASS_THROW("unsupported");
159 /*TPimpl pimpl(
160 new impl::MultiCallbackImpl<CallbackType>(p,true));
161 init(pimpl);*/
162 }
163 template<typename CallbackType> MultiCallback( const CallbackType& callback )
164 {
165 util::SharedPtr<CallbackType> p(new CallbackType(callback));
166#pragma LASS_FIXME("This should be the read-only version")
167 TPimpl pimpl(new impl::MultiCallbackImpl<CallbackType>(p,false));
168 init(std::move(pimpl));
169 }
170 ~MultiCallback();
171
172 std::string repr(void) const;
173 void reset();
174
175 void call(const python::TPyObjPtr& args);
176 void add(const python::TPyObjPtr& args);
177 PyObject* callVar(PyObject* args);
178 PyObject* addVar(PyObject* args);
179
180 static Py_ssize_t length(PyObject* self);
181
182 const std::type_info& type() const;
183 void* raw(bool writable) const;
184
185 static PyObject* _tp_call(PyObject*, PyObject*, PyObject*);
186
187private:
188
189 typedef std::unique_ptr<impl::MultiCallbackImplBase> TPimpl;
190
191 MultiCallback(TPimpl&& pimpl);
192 void init(TPimpl&& pimpl);
193
194 TPimpl pimpl_;
195};
196
197namespace impl
198{
199 template <typename Callback>
200 struct CallIntermediateShadowTraits
201 {
202 typedef Callback TCallback;
203 typedef const Callback* TConstCallbackPtr;
204
205 typedef TCallback TCppClass;
206 typedef TConstCallbackPtr TConstCppClassPtr;
207
208 static int getObject(PyObject* self, TConstCppClassPtr& forCalling)
209 {
210 lass::util::SharedPtr<TCallback>* almost = static_cast<lass::util::SharedPtr<TCallback>*>(static_cast<MultiCallback*>(self)->raw(false));
211 forCalling = almost->get();
212 return 0;
213 }
214 };
215
216 template <typename C>
217 void MultiCallbackImpl<C>::call(const python::TPyObjPtr& args, PyObject* self)
218 {
219 LockGIL LASS_UNUSED(lock); // as we're going to release it again ...
220 impl::CallMethod< CallIntermediateShadowTraits<C> >::call(args.get(), self, &C::call );
221 }
222}
223
224}
225}
226
228
229#endif
230
231// EOF
use as base class if derived should not be copyable
void fetchAndThrowPythonException(std::string loc)
Fetch the current Python exception and throw it as a C++ PythonException.
PyObjectPtr< PyObject >::Type TPyObjPtr
PyObjectPtr to a PyObject.
#define PY_HEADER(t_parentClass)
Place as first line of your Pythonized class.
Comprehensive C++ to Python binding library.
Library for Assembled Shared Sources.
Definition config.h:53