Library of Assembled Shared Sources
 
Loading...
Searching...
No Matches
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-2026 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 LockGIL lock;
115 Callback tempCallback;
116 if (decodeTuple(args, tempCallback) != 0)
117 {
118 impl::fetchAndThrowPythonException(LASS_PRETTY_FUNCTION);
119 }
120 for (size_t i=0;i<tempCallback.size();++i)
121 callback_->add(tempCallback[i]);
122 }
123 protected:
124 TCallbackPtr callback_;
125 bool readOnly_;
126 };
127}
128
129
130
131class MultiCallback;
132typedef PyObjectPtr<MultiCallback>::Type TMultiCallbackPtr;
133
134/** MultiCallback. Object for interfacing MultiCallbacks with Python
135*/
136class LASS_PYTHON_DLL MultiCallback : public PyObjectPlus, util::NonCopyable
137{
138 PY_HEADER(PyObjectPlus)
139public:
140 template <typename CallbackType> MultiCallback( const util::SharedPtr<CallbackType>& callback)
141 {
142 // [TODO] untested
143 LASS_THROW("unsupported");
144 TPimpl pimpl(
145 new impl::MultiCallbackImpl<CallbackType>(callback,false));
146 init(std::move(pimpl));
147 }
148 template <typename CallbackType> MultiCallback( const util::SharedPtr<const CallbackType>& /*callback*/)
149 {
150 // [TODO] untested
151 LASS_THROW("unsupported");
152 /*TPimpl pimpl(
153 new impl::MultiCallbackImpl<CallbackType>(p,true));
154 init(pimpl);*/
155 }
156 template<typename CallbackType> MultiCallback( const CallbackType& callback )
157 {
158 util::SharedPtr<CallbackType> p(new CallbackType(callback));
159#pragma LASS_FIXME("This should be the read-only version")
160 TPimpl pimpl(new impl::MultiCallbackImpl<CallbackType>(p,false));
161 init(std::move(pimpl));
162 }
163 ~MultiCallback();
164
165 std::string repr(void) const;
166 void reset();
167
168 void call(const python::TPyObjPtr& args);
169 void add(const python::TPyObjPtr& args);
170 PyObject* callVar(PyObject* args);
171 PyObject* addVar(PyObject* args);
172
173 static Py_ssize_t length(PyObject* self);
174
175 const std::type_info& type() const;
176 void* raw(bool writable) const;
177
178 static PyObject* _tp_call(PyObject*, PyObject*, PyObject*);
179
180private:
181
182 typedef std::unique_ptr<impl::MultiCallbackImplBase> TPimpl;
183
184 MultiCallback(TPimpl&& pimpl);
185 void init(TPimpl&& pimpl);
186
187 TPimpl pimpl_;
188};
189
190namespace impl
191{
192 template <typename Callback>
193 struct CallIntermediateShadowTraits
194 {
195 typedef Callback TCallback;
196 typedef const Callback* TConstCallbackPtr;
197
198 typedef TCallback TCppClass;
199 typedef TConstCallbackPtr TConstCppClassPtr;
200
201 static int getObject(PyObject* self, TConstCppClassPtr& forCalling)
202 {
203 lass::util::SharedPtr<TCallback>* almost = static_cast<lass::util::SharedPtr<TCallback>*>(static_cast<MultiCallback*>(self)->raw(false));
204 forCalling = almost->get();
205 return 0;
206 }
207 };
208
209 template <typename C>
210 void MultiCallbackImpl<C>::call(const python::TPyObjPtr& args, PyObject* self)
211 {
212 LockGIL lock; // as we're going to release it again ...
213 impl::CallMethod< CallIntermediateShadowTraits<C> >::call(args.get(), self, &C::call );
214 }
215}
216
217}
218}
219
221
222#endif
223
224// 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