Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / pyuno / source / module / pyuno_iterator.cxx
bloba7862857d719d2677e191c4f4e35252fc3a310e7
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;
114 static PyTypeObject PyUNO_iterator_Type =
116 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
117 "PyUNO_iterator",
118 sizeof (PyUNO_iterator),
120 PyUNO_iterator_del,
121 #if PY_VERSION_HEX >= 0x03080000
122 0, // Py_ssize_t tp_vectorcall_offset
123 #else
124 nullptr, // printfunc tp_print
125 #endif
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 nullptr,
139 Py_TPFLAGS_HAVE_ITER,
140 nullptr,
141 nullptr,
142 nullptr,
143 nullptr,
145 PyUNO_iterator_iter, // Generic, reused between the iterator types
146 PyUNO_iterator_next,
147 nullptr,
148 nullptr,
149 nullptr,
150 nullptr,
151 nullptr,
152 nullptr,
153 nullptr,
155 nullptr,
156 nullptr,
157 nullptr,
158 nullptr,
159 nullptr,
160 nullptr,
161 nullptr,
162 nullptr,
163 nullptr,
164 nullptr,
165 nullptr,
167 #if PY_VERSION_HEX >= 0x03040000
168 , nullptr
169 #if PY_VERSION_HEX >= 0x03080000
170 , nullptr // vectorcallfunc tp_vectorcall
171 #endif
172 #endif
175 PyObject* PyUNO_iterator_new( const Reference< XEnumeration >& xEnumeration )
177 PyUNO_iterator* self = PyObject_New( PyUNO_iterator, &PyUNO_iterator_Type );
178 if ( self == nullptr )
179 return nullptr; // == error
180 self->members = new PyUNO_iterator_Internals;
181 self->members->xEnumeration = xEnumeration;
182 return reinterpret_cast<PyObject*>(self);
185 ///////////////////////////////////////////////////////////////////////////////
187 static void PyUNO_list_iterator_del( PyObject* self )
189 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
192 PyThreadDetach antiguard;
193 delete me->members;
195 PyObject_Del( self );
199 static PyObject* PyUNO_list_iterator_next( PyObject *self )
201 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
203 Runtime runtime;
204 Any aRet;
205 bool noMoreElements = false;
210 PyThreadDetach antiguard;
211 try {
212 aRet = me->members->xIndexAccess->getByIndex( me->members->index );
214 catch( const css::lang::IndexOutOfBoundsException & )
216 noMoreElements = true;
220 if ( noMoreElements )
222 PyErr_SetString( PyExc_StopIteration, "" );
223 return nullptr;
226 PyRef rRet = runtime.any2PyObject( aRet );
227 me->members->index++;
228 return rRet.getAcquired();
230 catch( css::script::CannotConvertException &e )
232 raisePyExceptionWithAny( css::uno::makeAny( e ) );
234 catch( css::lang::IllegalArgumentException &e )
236 raisePyExceptionWithAny( css::uno::makeAny( e ) );
238 catch( const css::lang::WrappedTargetException &e )
240 raisePyExceptionWithAny( css::uno::makeAny( e ) );
242 catch( const css::uno::RuntimeException &e )
244 raisePyExceptionWithAny( css::uno::makeAny( e ) );
247 return nullptr;
251 static PyTypeObject PyUNO_list_iterator_Type =
253 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
254 "PyUNO_iterator",
255 sizeof (PyUNO_list_iterator),
257 PyUNO_list_iterator_del,
258 #if PY_VERSION_HEX >= 0x03080000
259 0, // Py_ssize_t tp_vectorcall_offset
260 #else
261 nullptr, // printfunc tp_print
262 #endif
263 nullptr,
264 nullptr,
265 nullptr,
266 nullptr,
267 nullptr,
268 nullptr,
269 nullptr,
270 nullptr,
271 nullptr,
272 nullptr,
273 nullptr,
274 nullptr,
275 nullptr,
276 Py_TPFLAGS_HAVE_ITER,
277 nullptr,
278 nullptr,
279 nullptr,
280 nullptr,
282 PyUNO_iterator_iter, // Generic, reused between the iterator types
283 PyUNO_list_iterator_next,
284 nullptr,
285 nullptr,
286 nullptr,
287 nullptr,
288 nullptr,
289 nullptr,
290 nullptr,
292 nullptr,
293 nullptr,
294 nullptr,
295 nullptr,
296 nullptr,
297 nullptr,
298 nullptr,
299 nullptr,
300 nullptr,
301 nullptr,
302 nullptr,
304 #if PY_VERSION_HEX >= 0x03040000
305 , nullptr
306 #if PY_VERSION_HEX >= 0x03080000
307 , nullptr // vectorcallfunc tp_vectorcall
308 #endif
309 #endif
312 PyObject* PyUNO_list_iterator_new( const Reference<XIndexAccess> &xIndexAccess )
314 PyUNO_list_iterator* self = PyObject_New( PyUNO_list_iterator, &PyUNO_list_iterator_Type );
315 if ( self == nullptr )
316 return nullptr; // == error
317 self->members = new PyUNO_list_iterator_Internals;
318 self->members->xIndexAccess = xIndexAccess;
319 self->members->index = 0;
320 return reinterpret_cast<PyObject*>(self);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */