tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / pyuno / source / module / pyuno_iterator.cxx
blob2fc70a32fcff841f4603c40629781ea933203187
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::Any( e ) );
93 catch( css::script::CannotConvertException &e )
95 raisePyExceptionWithAny( css::uno::Any( e ) );
97 catch( css::lang::IllegalArgumentException &e )
99 raisePyExceptionWithAny( css::uno::Any( e ) );
101 catch( const css::lang::WrappedTargetException &e )
103 raisePyExceptionWithAny( css::uno::Any( e ) );
105 catch( const css::uno::RuntimeException &e )
107 raisePyExceptionWithAny( css::uno::Any( 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 #if PY_VERSION_HEX >= 0x030C00A1
181 , 0 // tp_watched
182 #endif
183 #endif
184 #endif
187 PyObject* PyUNO_iterator_new( const Reference< XEnumeration >& xEnumeration )
189 PyUNO_iterator* self = PyObject_New( PyUNO_iterator, &PyUNO_iterator_Type );
190 if ( self == nullptr )
191 return nullptr; // == error
192 self->members = new PyUNO_iterator_Internals;
193 self->members->xEnumeration = xEnumeration;
194 return reinterpret_cast<PyObject*>(self);
197 ///////////////////////////////////////////////////////////////////////////////
199 static void PyUNO_list_iterator_del( PyObject* self )
201 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
204 PyThreadDetach antiguard;
205 delete me->members;
207 PyObject_Del( self );
211 static PyObject* PyUNO_list_iterator_next( PyObject *self )
213 PyUNO_list_iterator* me = reinterpret_cast<PyUNO_list_iterator*>(self);
215 Runtime runtime;
216 Any aRet;
220 bool noMoreElements = false;
222 PyThreadDetach antiguard;
223 try {
224 aRet = me->members->xIndexAccess->getByIndex( me->members->index );
226 catch( const css::lang::IndexOutOfBoundsException & )
228 noMoreElements = true;
232 if ( noMoreElements )
234 PyErr_SetString( PyExc_StopIteration, "" );
235 return nullptr;
238 PyRef rRet = runtime.any2PyObject( aRet );
239 me->members->index++;
240 return rRet.getAcquired();
242 catch( css::script::CannotConvertException &e )
244 raisePyExceptionWithAny( css::uno::Any( e ) );
246 catch( css::lang::IllegalArgumentException &e )
248 raisePyExceptionWithAny( css::uno::Any( e ) );
250 catch( const css::lang::WrappedTargetException &e )
252 raisePyExceptionWithAny( css::uno::Any( e ) );
254 catch( const css::uno::RuntimeException &e )
256 raisePyExceptionWithAny( css::uno::Any( e ) );
259 return nullptr;
262 static PyTypeObject PyUNO_list_iterator_Type =
264 PyVarObject_HEAD_INIT( &PyType_Type, 0 )
265 "PyUNO_iterator",
266 sizeof (PyUNO_list_iterator),
268 PyUNO_list_iterator_del,
269 #if PY_VERSION_HEX >= 0x03080000
270 0, // Py_ssize_t tp_vectorcall_offset
271 #else
272 nullptr, // printfunc tp_print
273 #endif
274 nullptr,
275 nullptr,
276 nullptr,
277 nullptr,
278 nullptr,
279 nullptr,
280 nullptr,
281 nullptr,
282 nullptr,
283 nullptr,
284 nullptr,
285 nullptr,
286 nullptr,
287 Py_TPFLAGS_HAVE_ITER,
288 nullptr,
289 nullptr,
290 nullptr,
291 nullptr,
293 PyUNO_iterator_iter, // Generic, reused between the iterator types
294 PyUNO_list_iterator_next,
295 nullptr,
296 nullptr,
297 nullptr,
298 nullptr,
299 nullptr,
300 nullptr,
301 nullptr,
303 nullptr,
304 nullptr,
305 nullptr,
306 nullptr,
307 nullptr,
308 nullptr,
309 nullptr,
310 nullptr,
311 nullptr,
312 nullptr,
313 nullptr,
315 #if PY_VERSION_HEX >= 0x03040000
316 , nullptr
317 #if PY_VERSION_HEX >= 0x03080000
318 , nullptr // vectorcallfunc tp_vectorcall
319 #if PY_VERSION_HEX < 0x03090000
320 #if defined __clang__
321 #pragma clang diagnostic push
322 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
323 #endif
324 , nullptr // tp_print
325 #if defined __clang__
326 #pragma clang diagnostic pop
327 #endif
328 #endif
329 #if PY_VERSION_HEX >= 0x030C00A1
330 , 0 // tp_watched
331 #endif
332 #endif
333 #endif
336 PyObject* PyUNO_list_iterator_new( const Reference<XIndexAccess> &xIndexAccess )
338 PyUNO_list_iterator* self = PyObject_New( PyUNO_list_iterator, &PyUNO_list_iterator_Type );
339 if ( self == nullptr )
340 return nullptr; // == error
341 self->members = new PyUNO_list_iterator_Internals;
342 self->members->xIndexAccess = xIndexAccess;
343 self->members->index = 0;
344 return reinterpret_cast<PyObject*>(self);
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */