Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / pyuno / source / module / pyuno_iterator.cxx
blobb5dcf78695a8e04cb2996b83bd717653e1352f31
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include "pyuno_impl.hxx"
24 #include <com/sun/star/container/XEnumeration.hpp>
25 #include <com/sun/star/container/XIndexAccess.hpp>
26 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <com/sun/star/lang/WrappedTargetException.hpp>
28 #include <com/sun/star/script/CannotConvertException.hpp>
30 using com::sun::star::container::XEnumeration;
31 using com::sun::star::container::XIndexAccess;
32 using com::sun::star::lang::IndexOutOfBoundsException;
33 using com::sun::star::lang::WrappedTargetException;
34 using com::sun::star::uno::Any;
35 using com::sun::star::uno::Reference;
36 using com::sun::star::uno::RuntimeException;
39 namespace pyuno
42 static void PyUNO_iterator_del( PyObject* self )
44 PyUNO_iterator* me = reinterpret_cast<PyUNO_iterator*>(self);
47 PyThreadDetach antiguard;
48 delete me->members;
50 PyObject_Del( self );
53 static PyObject* PyUNO_iterator_iter( PyObject *self )
55 Py_INCREF( self );
56 return self;
59 static PyObject* PyUNO_iterator_next( PyObject *self )
61 PyUNO_iterator* me = reinterpret_cast<PyUNO_iterator*>(self);
63 Runtime runtime;
64 Any aRet;
66 try
68 bool hasMoreElements = false;
71 PyThreadDetach antiguard;
73 hasMoreElements = me->members->xEnumeration->hasMoreElements();
74 if ( hasMoreElements )
76 aRet = me->members->xEnumeration->nextElement();
80 if ( hasMoreElements )
82 PyRef rRet = runtime.any2PyObject( aRet );
83 return rRet.getAcquired();
86 PyErr_SetString( PyExc_StopIteration, "" );
87 return nullptr;
89 catch( css::container::NoSuchElementException &e )
91 raisePyExceptionWithAny( css::uno::makeAny( e ) );
93 catch( css::script::CannotConvertException &e )
95 raisePyExceptionWithAny( css::uno::makeAny( e ) );
97 catch( css::lang::IllegalArgumentException &e )
99 raisePyExceptionWithAny( css::uno::makeAny( e ) );
101 catch( const css::lang::WrappedTargetException &e )
103 raisePyExceptionWithAny( css::uno::makeAny( e ) );
105 catch( const css::uno::RuntimeException &e )
107 raisePyExceptionWithAny( css::uno::makeAny( e ) );
110 return nullptr;
113 static PyTypeObject PyUNO_iterator_Type =
115 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
116 "PyUNO_iterator",
117 sizeof (PyUNO_iterator),
119 PyUNO_iterator_del,
120 #if PY_VERSION_HEX >= 0x03080000
121 0, // Py_ssize_t tp_vectorcall_offset
122 #else
123 nullptr, // printfunc tp_print
124 #endif
125 nullptr,
126 nullptr,
127 nullptr,
128 nullptr,
129 nullptr,
130 nullptr,
131 nullptr,
132 nullptr,
133 nullptr,
134 nullptr,
135 nullptr,
136 nullptr,
137 nullptr,
138 Py_TPFLAGS_HAVE_ITER,
139 nullptr,
140 nullptr,
141 nullptr,
142 nullptr,
144 PyUNO_iterator_iter, // Generic, reused between the iterator types
145 PyUNO_iterator_next,
146 nullptr,
147 nullptr,
148 nullptr,
149 nullptr,
150 nullptr,
151 nullptr,
152 nullptr,
154 nullptr,
155 nullptr,
156 nullptr,
157 nullptr,
158 nullptr,
159 nullptr,
160 nullptr,
161 nullptr,
162 nullptr,
163 nullptr,
164 nullptr,
166 #if PY_VERSION_HEX >= 0x03040000
167 , nullptr
168 #if PY_VERSION_HEX >= 0x03080000
169 , nullptr // vectorcallfunc tp_vectorcall
170 #if PY_VERSION_HEX < 0x03090000
171 #if defined __clang__
172 #pragma clang diagnostic push
173 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
174 #endif
175 , nullptr // tp_print
176 #if defined __clang__
177 #pragma clang diagnostic pop
178 #endif
179 #endif
180 #endif
181 #endif
184 PyObject* PyUNO_iterator_new( const Reference< XEnumeration >& xEnumeration )
186 PyUNO_iterator* self = PyObject_New( PyUNO_iterator, &PyUNO_iterator_Type );
187 if ( self == nullptr )
188 return nullptr; // == error
189 self->members = new PyUNO_iterator_Internals;
190 self->members->xEnumeration = xEnumeration;
191 return reinterpret_cast<PyObject*>(self);
194 ///////////////////////////////////////////////////////////////////////////////
196 static void PyUNO_list_iterator_del( PyObject* self )
198 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
201 PyThreadDetach antiguard;
202 delete me->members;
204 PyObject_Del( self );
208 static PyObject* PyUNO_list_iterator_next( PyObject *self )
210 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
212 Runtime runtime;
213 Any aRet;
217 bool noMoreElements = false;
219 PyThreadDetach antiguard;
220 try {
221 aRet = me->members->xIndexAccess->getByIndex( me->members->index );
223 catch( const css::lang::IndexOutOfBoundsException & )
225 noMoreElements = true;
229 if ( noMoreElements )
231 PyErr_SetString( PyExc_StopIteration, "" );
232 return nullptr;
235 PyRef rRet = runtime.any2PyObject( aRet );
236 me->members->index++;
237 return rRet.getAcquired();
239 catch( css::script::CannotConvertException &e )
241 raisePyExceptionWithAny( css::uno::makeAny( e ) );
243 catch( css::lang::IllegalArgumentException &e )
245 raisePyExceptionWithAny( css::uno::makeAny( e ) );
247 catch( const css::lang::WrappedTargetException &e )
249 raisePyExceptionWithAny( css::uno::makeAny( e ) );
251 catch( const css::uno::RuntimeException &e )
253 raisePyExceptionWithAny( css::uno::makeAny( e ) );
256 return nullptr;
259 static PyTypeObject PyUNO_list_iterator_Type =
261 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
262 "PyUNO_iterator",
263 sizeof (PyUNO_list_iterator),
265 PyUNO_list_iterator_del,
266 #if PY_VERSION_HEX >= 0x03080000
267 0, // Py_ssize_t tp_vectorcall_offset
268 #else
269 nullptr, // printfunc tp_print
270 #endif
271 nullptr,
272 nullptr,
273 nullptr,
274 nullptr,
275 nullptr,
276 nullptr,
277 nullptr,
278 nullptr,
279 nullptr,
280 nullptr,
281 nullptr,
282 nullptr,
283 nullptr,
284 Py_TPFLAGS_HAVE_ITER,
285 nullptr,
286 nullptr,
287 nullptr,
288 nullptr,
290 PyUNO_iterator_iter, // Generic, reused between the iterator types
291 PyUNO_list_iterator_next,
292 nullptr,
293 nullptr,
294 nullptr,
295 nullptr,
296 nullptr,
297 nullptr,
298 nullptr,
300 nullptr,
301 nullptr,
302 nullptr,
303 nullptr,
304 nullptr,
305 nullptr,
306 nullptr,
307 nullptr,
308 nullptr,
309 nullptr,
310 nullptr,
312 #if PY_VERSION_HEX >= 0x03040000
313 , nullptr
314 #if PY_VERSION_HEX >= 0x03080000
315 , nullptr // vectorcallfunc tp_vectorcall
316 #if PY_VERSION_HEX < 0x03090000
317 #if defined __clang__
318 #pragma clang diagnostic push
319 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
320 #endif
321 , nullptr // tp_print
322 #if defined __clang__
323 #pragma clang diagnostic pop
324 #endif
325 #endif
326 #endif
327 #endif
330 PyObject* PyUNO_list_iterator_new( const Reference<XIndexAccess> &xIndexAccess )
332 PyUNO_list_iterator* self = PyObject_New( PyUNO_list_iterator, &PyUNO_list_iterator_Type );
333 if ( self == nullptr )
334 return nullptr; // == error
335 self->members = new PyUNO_list_iterator_Internals;
336 self->members->xIndexAccess = xIndexAccess;
337 self->members->index = 0;
338 return reinterpret_cast<PyObject*>(self);
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */