1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
42 static void PyUNO_iterator_del( PyObject
* self
)
44 PyUNO_iterator
* me
= reinterpret_cast<PyUNO_iterator
*>(self
);
47 PyThreadDetach antiguard
;
53 static PyObject
* PyUNO_iterator_iter( PyObject
*self
)
59 static PyObject
* PyUNO_iterator_next( PyObject
*self
)
61 PyUNO_iterator
* me
= reinterpret_cast<PyUNO_iterator
*>(self
);
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
, "" );
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
) );
113 static PyTypeObject PyUNO_iterator_Type
=
115 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
117 sizeof (PyUNO_iterator
),
120 #if PY_VERSION_HEX >= 0x03080000
121 0, // Py_ssize_t tp_vectorcall_offset
123 nullptr, // printfunc tp_print
138 Py_TPFLAGS_HAVE_ITER
,
144 PyUNO_iterator_iter
, // Generic, reused between the iterator types
166 #if PY_VERSION_HEX >= 0x03040000
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"
175 , nullptr // tp_print
176 #if defined __clang__
177 #pragma clang diagnostic pop
180 #if PY_VERSION_HEX >= 0x030C00A1
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
;
207 PyObject_Del( self
);
211 static PyObject
* PyUNO_list_iterator_next( PyObject
*self
)
213 PyUNO_list_iterator
* me
= reinterpret_cast<PyUNO_list_iterator
*>(self
);
220 bool noMoreElements
= false;
222 PyThreadDetach antiguard
;
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
, "" );
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
) );
262 static PyTypeObject PyUNO_list_iterator_Type
=
264 PyVarObject_HEAD_INIT( &PyType_Type
, 0 )
266 sizeof (PyUNO_list_iterator
),
268 PyUNO_list_iterator_del
,
269 #if PY_VERSION_HEX >= 0x03080000
270 0, // Py_ssize_t tp_vectorcall_offset
272 nullptr, // printfunc tp_print
287 Py_TPFLAGS_HAVE_ITER
,
293 PyUNO_iterator_iter
, // Generic, reused between the iterator types
294 PyUNO_list_iterator_next
,
315 #if PY_VERSION_HEX >= 0x03040000
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"
324 , nullptr // tp_print
325 #if defined __clang__
326 #pragma clang diagnostic pop
329 #if PY_VERSION_HEX >= 0x030C00A1
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: */